Solved

Static keyword in java

Posted on 2004-09-02
8
7,206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

688 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