Solved

Static keyword in java

Posted on 2004-09-02
8
7,198 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now