File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Variables in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variables in Java" Watch "Variables in Java" New topic

Variables in Java

Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 94

What is the difference between Instance Variables, Local Variables and Class variables in Java.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

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

Joined: Sep 28, 2008
Posts: 324

SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 94

Hi All,

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

System.out.println("coordinates of p2 are



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

Joined: Aug 16, 2005
Posts: 15082

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 94

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

Joined: Feb 17, 2008
Posts: 94

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

Joined: Nov 14, 2008
Posts: 18
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

Joined: Oct 13, 2005
Posts: 46349
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.
I agree. Here's the link:
subject: Variables in Java
It's not a secret anymore!