what is difference between Class variables and Instance variables?

F

fartymcfly

hello,
what is difference between Class variables and Instance variables?

A class variable is visible/usable to all methods within that class. A
instance variable is only visible/usable within the method (instance) that
it has been declared in. Its all about variable scope my friend!

regards,

fartymcfly
 
B

Benoy

Class Variable in Java

In Java Variables are of many types
out of that these are two

Class Variable

As you told about class variables they can be used any where in that
class
but, as java is an object oriented so the variables can be used in
other classes by creating objects

thank you
Benoy
 
M

Matt Humphrey

hello,
what is difference between Class variables and Instance variables?

Instance variables belong to the object instance. They're the fields of the
object. Class variables belong to the class as a whole--they're static
variables. I recommend Roedy's definitions here:

http://mindprod.com/jgloss/instancevariable.html or if you're up to it the
JLS definition here
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#28536

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
R

Roedy Green

A class variable is visible/usable to all methods within that class. A
instance variable is only visible/usable within the method (instance) that
it has been declared in. Its all about variable scope my friend!

No. You have instance variables confused with local variables.
 
V

Virgil Green

hello,
what is difference between Class variables and Instance variables?

Class variables are more broad-minded than their narrow-minded cousins,
Instance variables.
 
Joined
Feb 18, 2008
Messages
1
Reaction score
0
Static variable, the field is allocated when the class is created. It belongs to the class and not any object of the class. It is class variable.

Instace variable, the field is allocated when the class is instanciated to the class is called instance variable or non-static variable

Static variable has only one copy for all the methods in class while instance variable has many copy.

Class can access only static variable while object can access both class and instance variable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top