Solved

Static keyword in java

Posted on 2004-09-02
8
7,202 Views
Last Modified: 2012-08-13
what is the significance of static keyword in java?.
why do we use it for methods, variables etc?
could u explain me in detail and possibly with some examples?


thanks and regards,
sameer
0
Comment
Question by:SamiirKhan
8 Comments
 
LVL 19

Accepted Solution

by:
ramazanyich earned 25 total points
ID: 11970702
vor variables static keyword means that it is assigned for all instances of class.
For methods statis means that you can call it without creating instance of class.
Example:
in class A you have method:
public statis void method test(){
...
}

So you can call it from another class B:
A.test();

For variables:
Class A has
public static String ss=" test";

In another class you have defined:
Object a = new A();
Object b= new A();

a.ss="another value";
System.out.println(b.ss);

output should be "another value"
0
 
LVL 3

Assisted Solution

by:aravindtj
aravindtj earned 25 total points
ID: 11973790
hi,
Static varialbles/ methods are being only one instance.
Even though you have multiple instances of a object, having static variables/ methods,
there is a only one instance for all those instances.
Static varibales are initialized automatically. Eg. static int a; means a=0, and if its value is modified in any of the instance, it will affected on all the instances.
You can access those static variables/methods with out creating instances of the object.

all the best.
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 25 total points
ID: 11974253
Dear sameer,

Static variable and method belongs to the class and not to the instance of the class. So everytime you create the instance of class, the static variable will remain the same.

Why do you need them? Well, sometimes that you want to have the counter on how many classes instance have been created. This static variable will help you to do it. Static is also useful when you really want to have a single instance of the class to be created. By using static, you can prohibit the user from having more than one instance for one object.

Remember that static method will not be able to be overriden.

I hope it helps

 

 
0
 
LVL 15

Expert Comment

by:babuno5
ID: 11981134
Well as we know that  a class can contain methods and variables that are shared among all instances of the class. These shared members are called static variables and static methods. The most common use of static variables in a class is to hold predefined constants or unchanging objects all of the instances can use.
There are two advantages to this approach. The more obvious advantage is that static members take up space
only in the class; the members are not replicated in each instance. The second advantage is that static
members can be accessed even if no instances of the class exist. A hypothetical Component class might
have a static variable called maximumWidth. Some other class that has to deal with this component, such
as a layout manager, might want to know the maximum width of such a component, even if there aren't any
around at the moment. Since maximumWidth is a static variable, the layout manager can get this
information.
An instance of the Color class represents a color. For convenience, the Color class contains some static,
predefined color objects with friendly names like green, red, and (my favorite) magenta.
Color.green is thus a predefined Color object that is set to a green color. In this case, these static
members of Color are not changeable, so they are effectively constants and can be optimized as such by the
compiler. Constant static members make up for the lack of a #define construct in Java.
0
 
LVL 6

Expert Comment

by:SCDMETA
ID: 11991628
An easy way to think about static variables and methods is:

1) They are like global variables in a class.  All instances of the class share the same "static" variables.
2) The variables and methods exist and can be accessed via the class even though an instances of the class does not exist.



0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 105
Advice in Xamarin 21 97
ejb example issues 3 29
Assignment from incompatible pointer type? 2 13
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question