answersLogoWhite

0


Best Answer

The main difference between the class variable and Instance variable is,

first time, when class is loaded in to memory, then only memory is allocated for all class variables. Usually static variables are called class variables. These variables are available throughout the execution of the application and the values are common to the class. You can access them directly without creating an object of the class.

Instance variables are normal variables declared in a class, that would get initialized when you create an instance of the class. Every instance of the class would have a copy of the variable and you need a class instance (object) to access these variables

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Well, at the basic level, class fields, or static fields, as they are called sometimes, belong to the class and not to any particular instance of the class. Class fields can be assigned without instantiation of instance objects of a class. For example:

public class ExampleClass {

public static String classField = "";

public String instanceField = "";

}

I can assign a value to classField just like that:

ExampleClass.classField = "new value";

Instance fields, on the other hand, belong to a particular instance of the class. Thus, to assign an instance field a value, you have to first instantiate an object of the class just like that:

ExampleClass obj1 = new ExampleClass();

obj1.instanceField = "new value";

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Static Variables or Class Variables

The static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.

Instance Variables

The instance variables dont belong to the class but instead are mapped to every single instance of the class object.

Ex:

public class Test {

private static String var = "AAA";

private int n = 10;

}

in the code above var is a class variable whereas n is an instance variable

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

In java, any variable marked as a static is considered a variable owned by the class. Even if you have a bunch of instances of this class, you only will have one copy of your static variable shared by all the instances.

This is the main difference with instance variables where each instance of your class have his own variables.

Example, if you have a class like this:

public class SoldCar{

public String model; //instance variable - each soldCar have its model

public int cost; //instance variable - each soldCar have its cost

public static int count; //class variable - all soldCars share this count

public SoldCar(){

count ++;

}

public static void main(String[] args){

SoldCar ferrari = new SoldCar();

ferrari.model = "fiorano";

ferrari .cost = 999999;

SoldCar beetle = new SoldCar();

beetle.model = "new beetle";

beetle.cost = 2222;

System.out.println( ferrari.model + "," + ferrari.count );

System.out.println( beetle.model + "," + beetle.count );

}

}

The output of this program is :

fiorano,2

new beetle,2

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Instance Variable: These are the normal variables that you declare within a class. These variables are tagged to a particular object instance of the class.

Class Variables: These are the static variables that you declare within a class. These variables are not tagged to a particular object instance of a class and no matter how many object instances get created for a class, only one instance of the static variable gets created.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

instance variables:the variables which are declared in class

these variables can have access modifiers

local variables: the variables which are declared in method

these variables should not have access modifiers/specifiers except final

answer by divya puvvada

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is difference between instance variable and class variable in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is mean by instance variable with example?

hi friend.... Instance variable means with which you need to create an obeject in order to invoke any methods or variables defined in a class. For ex: class A { int a=10; public void inc() {return a++; } } To invoke a member A b=new A(); b.inc(); System.out.println(b.a); If incase of a static method you can directly call using class name itself. like : b=Math.sqrt(a);


What is the difference between an attribute and a behavior in c plus plus?

An attribute is a class member variable while a behaviour is a class member method.


What is the function of default values in java?

Default values are available for any class or instance variable. If you do not specify a value for a class or instance variable the JVM will provide a default value that will ensure that the system does not end up with any unexpected errors because you used a variable that was not initialized. Ex: Public class Test { int I; } In the above class we have just declared an instance variable called 'I' but we haven't associated any value to it. The JVM automatically assigns 0 as the default value to this variable.


What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.


What does instance field mean in Java?

Fields defined without the "static" keyword.Their value is unique to each instance (object) of a class.AnswerInstance variable is a variable declared within the class for which every object of the class has its own value.

Related questions

What is difference between an instance variable and a class variable?

An instance variable is typically associated with an object instance of the class whereas class variable is not associated with any object instance. Static variables are referred to as class variables while non-static regular variables are called instance variables. Simply put, you will have as many instances of the instance variable as there are object instances. i.e., if there are 10 instances of an object, you will have 10 instances of that instance variable as well. But, there will be only one instance of the static or class variable. Instance variables are accessed as follows: objname.variableName; Class variables are accessed as follows: ClassName.variableName;


In oops what is the difference between class variable and variable?

Class Variable is a subset of Variables.


What is the difference between class variables and instance variables?

In the case of an instance variable, there is one copy for every instance (object). If you create 10 objects based on a class, there will be 10 copies of the variable. A class variable exists only once for the entire class - no matter how many objects you create - or even if you create no objects based on the class. In Java, such variables (class variables) are declared with the statickeyword.


What is difference between struct variable and class object?

structure variable can access the variable but class object can access the function also


What is Difference between local variable and data members or instance variable?

A data member belongs to an object of a class whereas local variable belongs to its current scope. A local variable is declared within the body of a function and can be used only from the point at which it is declared to the immediately following closing brace. A data member is declared in a class definition, but not in the body of any of the class member functions. Data members are accessible to all member function of the class.


What is importance of static variable?

The static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.


What is mean by instance variable with example?

hi friend.... Instance variable means with which you need to create an obeject in order to invoke any methods or variables defined in a class. For ex: class A { int a=10; public void inc() {return a++; } } To invoke a member A b=new A(); b.inc(); System.out.println(b.a); If incase of a static method you can directly call using class name itself. like : b=Math.sqrt(a);


What is the difference between classes and object in C?

An object is an INSTANCE of a class. Human is a class, while YOU are a person, an instance of Human class, but YOU do not represent the entire human class. Or, a class provides the abstraction, and an object is a typical example of that abstraction. Classes provide a blue print to build a real instance of an object.


What is the difference between an attribute and a behavior in c plus plus?

An attribute is a class member variable while a behaviour is a class member method.


What are the difference between private variable and final variable?

A private variable is one that is accessible only to the current class and cannot be accessed by any other class, including the ones that extend from it. A final variable is one that cannot be modified once it is initialized and assigned a value.


What is the function of default values in java?

Default values are available for any class or instance variable. If you do not specify a value for a class or instance variable the JVM will provide a default value that will ensure that the system does not end up with any unexpected errors because you used a variable that was not initialized. Ex: Public class Test { int I; } In the above class we have just declared an instance variable called 'I' but we haven't associated any value to it. The JVM automatically assigns 0 as the default value to this variable.


What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.