Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variables in Java

 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between Instance Variables, Local Variables and Class variables in Java.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried researching this yourself? Just about any beginning book on Java will explain it. Many web site tutorials will also have the answer. Doing the research yourself probably would have been faster that posting here and waiting for someone to give you the answer.

We really want to help people learn here - we don't just hand out answers. If you find something that doesn't make sense, by all means post what you find (cite your source!!!), and ask for clarification or a deeper explanation. That's the best way to learn.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok for one last time we will answer this question.

Instance variables are variables of a particular instance of a class. A copy of each of the instance variables is created when an instance of the class is created using the "new" keyword. They are valid as long as the instance is valid.

Local variables are variables that are local to a particular block of code. They are alive only during the execution of that particular block.

Class variables are static variables that are defined for a particular class. These variables are created during loading the class and only one copy of this variables are shared with all the instances of the class.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html
 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks for the explanation. I have some questions on this. Please help me understand the below
<code>
class coord
{
int x, y; //Instance Variables
public static void main(String[] args)
{
int z=40; //Local Variable
coord p1,p2;
p1=new coord();
p1.x=50;
p1.y=50;
p2=p1;
p1.x=100;
p1.y=75;
System.out.println("coordinates of p1 are

"+p1.x+","+p1.y);
System.out.println("coordinates of p2 are

"+p2.x+","+p2.y);
}
}

</code>

In the above code,I understand that the variables declared after the class definition is the Instance variable. and the variable 'z' declared inside the main method is the local variable.According to my understanding, this variable will be accessible only inside the main method. Correct me if im wrong.

coord p1,p2;

The above line in the program, is it declaring objects for the class coord?
p1=new coord();

Im not sure what the above line is doing. Is it creating the object p1 of the coord class? but the general syntax for object creation is

<classname> <object name> = new <call to constructor>.
Please explain. Many Thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are non-static variables which are declared directly inside the class definition (such as x and y in your example).

Class variables are static variables which are declared directly inside the class definition. The difference with instance variables is that there's a new copy of the instance variables for every object of that class, while with class variables there's just a single copy of the variable which is shared by all objects of the class.

Local variables are variables which are declared inside a method (such as z in your example). They only exist inside the method.

coord p1,p2;

This declares two variables, p1 and p2, but does not yet create any objects. Depending on if p1 and p2 are instance, class or local variables, they will be automatically initialized: Instance and class variables are automatically initialized to null, local variables are not automatically initialized and must be initialized before use (if you don't, the compiler will complain).

p1=new coord();

Here you create a new object of class coord, and you make variable p1 refer to the new object.

(Note that declaring variables and creating objects doesn't necessarily have to happen at the same time.)
[ November 26, 2008: Message edited by: Jesper Young ]
 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper . But in the above example, I have created only one object p1. But im able to access the values of x & y using p2. Im not sure how? There is only one object created for the class coord which is p1. But still the print statement is able to print the values of p2.x & p2.y.Im not clear how. Please explain
 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also im not sure, what type of variable is

Coord p1,p2;

Im not sure what type of variable is p1 and p2. Is it a local variable? since it is declared inside the main method
 
Narendira Sarma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure about p2 being able to print x and y values. But I guess line makes the trick. When p2 is referring to p1 and all changes made to p1 will reflect on p2.

Correct me if I am wrong.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narendira Sarma, you are correct. You have also found the code button ( :) ); please all other greenhorns use this button, not HTML tags, and maintain indentation otherwise the code is difficult to read.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic