aspose file tools*
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
Author

Variables in Java

Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

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

Joined: Oct 02, 2003
Posts: 11314
    
  16

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.


-kolli
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html


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

Joined: Feb 17, 2008
Posts: 93

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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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 7 API documentation
Scala Notes - My blog about Scala
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

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: 93

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

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
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
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: http://aspose.com/file-tools
 
subject: Variables in Java