Click to See Complete Forum and Search --> : new to java - is everything 'static'?
sharnik
02-26-2004, 03:00 PM
I've just started learning Java and encountered a problem (or I'm just imaginig it's a problem).
'main' method should be 'static' right? (they say 'public static void main() everywhere)
So, if you want to call another method you wrote in the same class, it should also be 'static', right? (I get [i]non-static method MyMethod cannot be referenced from a static context [\i] if I try to call a method that isn't static from inside the 'main' method)
So, all the methods that I can use in 'main' need to be static, right?
Is something wrong with it, because it seems odd (in this case it means all the methods that I use)?
Interesting fact - I have no problems calling System.out.print Does it mean it's also static?
Fryguy8
02-26-2004, 03:35 PM
Not really :)
You might want to learn exactly what static MEANS before going and saying that. Very few of the objects that I implement have static elements in them. Most things in main programs will be static methods though.
voidinit
02-26-2004, 05:32 PM
You can do some thing like this below to get around it.
public class foo{
foo(){
nonStaticMethod();
}
public static void main{
foo me = new foo();
}
void nonStaticMethod(){
//....do whatever...
}
}
sharnik
02-26-2004, 06:08 PM
Originally posted by Fryguy8
Very few of the objects that I implement have static elements in them. Most things in main programs will be static methods though.
I meant the methods mostly. My fault not to clarify ;)
voidinit - thx, for the example
bryan.6
02-26-2004, 08:09 PM
yeah, so in the beginning it'll seem like everything is static, because that's how you are calling it. if you do:
retval = MyMethod();
or even
MyMethod();
that's a static call. eventually, as you learn, you'll get into instance variables and such, which are like data types. then you'll be doing stuff like...
MyDataType mdt = new MyDataType();
mdt.MyMethod();
in that case, that's not a static method. but yeah, as you begin, you'll deal mostly with static methods.
by the way, yes, System.out.print() is a static method.
bwkaz
02-26-2004, 08:13 PM
Originally posted by sharnik
Interesting fact - I have no problems calling System.out.print Does it mean it's also static? Sort of, but not entirely. System.out is static, but print() is not. java.lang.System.out returns a reference to a java.io.PrintStream (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html), which has a print() method that isn't static.
ajdearson
03-02-2004, 04:29 AM
As far as methods are concerned 'static' means that the methods are available BEFORE and instances of the class exist. That's why 'main' is static since, when you start the program, no instances of the base class exits. I don't know of any restriction of static calls static or whatever. Keywords public, private etc govern what are available to call.
Static variables are a bit different. 'Normal' variables in a class have an instance (of the variable) for every instance of the class. However, a static variable will exist ONCE no matter how many instances of the class have been created -- it is a common variable between the instances -- one instance changes it means that all instances can see the changes
Tony
Hi sharnik
I sincerely recommend to post Java related questions to:
http://www.javaworld.com/javaforums/postlist.php?Cat=&Board=javabeginner
and/or
http://forum.java.sun.com/
public class Foo{
int nonStaticMethod(){
...
}
public static void main(String[] args){
Foo f = new Foo();
int n = f.nonStaticMethod();
}
}
bwkaz
03-02-2004, 08:24 PM
Originally posted by ajdearson
I don't know of any restriction of static calls static or whatever. But they do exist.
If you're inside a static method, you cannot call non-static methods without an explicit instance of the class. For example:
class Test {
public static void main(String args[]) {
Test t = new Test();
thiswillfail();
t.thiswillfail(); /* this will actually succeed, if the prev. line is commented out. */
}
private void thiswillfail() {
/* doesn't matter what goes in here, just that it's not static */
}
} The compilation will fail at the call to the thiswillfail() function, because main is static but thiswillfail() is not. Therefore, Java doesn't know which instance of Test to call thiswillfail on. In the second instance, there's an explicit instance of the Test class, so Java can use it to call thiswillfail() (and the thiswillfail() call does actually compile).