Static?
Author |
Message |
Horus
|
Posted: Sat Nov 22, 2008 2:09 pm Post subject: Static? |
|
|
I'm kinda new to Java, I just started this year.
I have a question about what static is, because my teacher never talked about it, the example he gave us sometimes contains static method sometimes doesn't but I really don't get what the difference is except static and non-static doesn't go together, I have to either make everything static in my program or everything non static or else it doesn't run.
But what exactly does static do? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
HellblazerX
![](http://www.plamania.co.kr/shopimages/plmtest/2910040000213.jpg)
|
Posted: Sat Nov 22, 2008 2:54 pm Post subject: Re: Static? |
|
|
A static method is a method that is called from the class, and not an object. For example:
Java: | public class Thing {
public static void action1 () {}
public void action2 () {}
} |
action1 () can be called without making an instance of the class:
Whereas action2 () can only be called from an instance:
Java: | Thing thing = new Thing ();
thing.action2 (); |
The reason the two don't go together is because you can't call on a static method from an instance of an object, and likewise you cannot call on a nonstatic method from just the class. |
|
|
|
|
![](images/spacer.gif) |
Horus
|
Posted: Sun Nov 23, 2008 11:02 am Post subject: RE:Static? |
|
|
so basically static is better right? saves less coding time.
then what's the point of using non static and why do they have that option in the first place? |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Sun Nov 23, 2008 11:24 am Post subject: RE:Static? |
|
|
Static is not better, it's just very different. By making everything static you ruin the whole point of object oriented programming, because classes define 'modules' rather than objects. If you don't understand this, keep looking into OOP, it takes time.
In the mean time, use the following as a general guideline: Use non-static members always unless absolutely necessary. A proper usage of static would be when something is associated with the class rather than an object of that class. Furthermore, avoid mixing static and non-static members and you'll be saving yourself many headaches. ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
S_Grimm
![](http://compsci.ca/v3/uploads/user_avatars/18926424384e6d86e5cf4d6.jpg)
|
Posted: Sun Nov 23, 2008 6:50 pm Post subject: RE:Static? |
|
|
for example
Java: |
public class Example1
public static void main (String [] args) throws Exception
{
//your code here
}
|
has to be static
Java: |
public class Example2
public void Example2 ()
{
//your code here
}
|
doesn't. the difference, like [Gandalf] said, is that I cannot call main from Example1 in Example2, whereas I can call Example2 in Example1. (Short version: Static is not always better, but always needed) |
|
|
|
|
![](images/spacer.gif) |
btiffin
![](http://compsci.ca/v3/uploads/user_avatars/189169540547b535e50e4a7.jpg)
|
|
|
|
![](images/spacer.gif) |
|
|