static and instance variables

Hi,

I read as below

Static members of class are used for storing the values that are the same
for all instances of class.


In the below statement
System.out.println("hi all");

out is a static member variable of the System class right. How are we able to call println method on satatic member variable of a system class.




Instance variables are alive till instance of the object xyz exists in the memory, that is the reason they are also called instance variables. Instance variablesshared and reused by all methods of the class, and even visible from externall classes also.

Static variables are alive after the instance object deleted(garbage collected) in the memory???. How instance variable life is different from static variable in this aspect.??

I am bit confused about static and instance variable which one to use in chich scenario and advantages, disadvantages, practical uses of each one.

Please advise
Any links resources ideas highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
Use a static variable when: You want the same value of the variable to be shared across all instances of a class. The value of the variable in this case will be the same for all instances of a class.

Use an instance variable when: You want each instance of the class to have its own copy of the variable. The value of the variable in this case will be different for each instance of a class.

For more info have a look at Oracle's web site.
0
phoffricCommented:
>> How are we able to call println method on satatic member variable of a system class.

bookmark ths:
http://docs.oracle.com/javase/7/docs/api/

left-hand  bottom pane.. scroll down to System - click on it:
Class System
scroll down to out and note that the type of out is PrintStream:
public static final PrintStream out
click on PrintStream
public class PrintStream
extends FilterOutputStream
implements Appendable, Closeable

...

public static final PrintStream out

The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.
For simple stand-alone Java applications, a typical way to write a line of output data is:

     System.out.println(data)
scroll down and notice that println is a method of class PrintStream
0
gudii9Author Commented:
>>System.out.println(data)


'out' is not a class right. It is member variable(like a,b) only of class named 'System' right. 'out' is not even a instance variable. How are we calling 'println' method on a member variable.(we cannot call something like a.abc() right)

public class Xyx{

public int a;
public  int b;
public  string method abc(){}
}


>>Instance variables are alive till instance of the object xyz exists in the memory

which make sense.

>>Static variables are alive after the instance object deleted(garbage collected) in the memory???. How instance variable life is different from static variable in this aspect.??

Till what time static variables are alive? whed do they die. Do they die similar to instance variables at any tme?



Please advise
0
phoffricCommented:
>> 'out' is not a class right.
right.
Please first review this:
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

>> It is member variable(like a,b) only of class named 'System' right.
Yes, out is a member variable of a class named 'System'.

>> 'out' is not even a instance variable.
True

>> How are we calling 'println' method on a member variable.

out is a member variable of the class type PrintStream which has a method called 'println'.

>> we cannot call something like a.abc() right
right. 'a' is an int. In your Xyz example, you need an instance of Xyz, call it, instanceXyz, and then you can have instanceXyz.abc().

>> Static variables are alive after the instance object deleted
True, but an instance need never be created and you can still refer to the class static member.

>> Do they die similar to instance variables at any tme?
no, instance variables die with the instance.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.