- Variables declared inside a class and outside method without static keyword known as instance variables.
- Instance variables will be used by objects to store state of the object.
- Every object will have their own copy of instance variables. where as static variables will be single and shared(accessed) among objects.
- Instance variables will be associated with the instance(Object)
- Static variables will be associated with the class.
#1: Java example program on declaring and accessing instance variables.
- package com.instanceofjava.instancevariables;
- /**
- * @author www.Instanceofjava.com
- * @category interview questions
- *
- * Description: Instance variables in java with example program
- *
- */
- public class InstanceVariables {
- String websiteName;
- String category;
- public static void main(String[] args) {
- InstanceVariables obj = new InstanceVariables();
- obj.websiteName="www.InstanceOfJava.com";
- obj.category="Java tutorial/interview questions";
- }
- }
- Instance variables allows all four type of access specifiers in java
- Instance variables can be final and transient
- Instance variables can not be declared as abstract, static strictfp, synchronized and native
No comments