Home > Archive > Java Help > November 2007 > main class and static
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
main class and static
|
|
|
| Hello
My program was running fine until I tried to add a timer (util.Timer) and
relevant code.
I then wanted to add a wait() in the main() , but the compiler told me that
a non static function can't be referenced from a static context (public
static main( args...)
Then I tried to remove static from the main() declaration, and from most
declaration in the source, but then the program refused to run, arguing "no
main class can't be found", although compiling with no error.
(I'm using Netbeans 5.5)
But yet the main class is defined in the properties (the program ran OK
before), and still appears in the properties / run / main class. (but a
browse in the main class tab option shows an empty list).
questions :
How to set up a main class when Netbeans can't see no one ?
Why is it necessary to declare the main() as static ?
How to use wait() in a static method ?
Denis
| |
| Gordon Beaton 2007-11-21, 4:35 am |
| On Wed, 21 Nov 2007 10:19:45 +0100, Snide wrote:
> Why is it necessary to declare the main() as static ?
Initially, there are no objects, so the first method called must be
static. From there you can create objects and then use those to call
non-static methods on those objects.
> How to use wait() in a static method ?
Just calling "wait()" implies "this.wait()", but because there is no
"this object" in a static context, you need to specify an object to
wait on, e.g.
someObject.wait();
I suspect you really want something like this instead:
Thread.sleep(someMilliseconds);
Note that wait() and sleep() are different; wait() waits for an event
(specifically, it waits for notify()) while sleep() simply waits for
time to pass.
Perhaps you need to explain *what* you are trying to do, not *how* you
are trying to do it.
/gordon
--
| |
| Ulrich Eckhardt 2007-11-21, 4:35 am |
| Snide wrote:
> I then wanted to add a wait() in the main() , but the compiler told me
> that a non static function can't be referenced from a static context
> (public static main( args...)
>
> Then I tried to remove static from the main() declaration, and from most
> declaration in the source, but then the program refused to run, arguing
> "no main class can't be found", although compiling with no error.
Always use cut'n'paste for error messages! This quote of yours for sure is
not from trying to run the program, just look at the double negation which
makes no sense but only leaves the people here to guess what might have
been meant.
[...]
> Why is it necessary to declare the main() as static ?
main() is the entry point for the program. It is not called as a
memberfunction, i.e. not in the context of any object, because there is no
object before it is entered. Only inside you create objects and then you
can invoke functions on them.
> How to use wait() in a static method?
The system probably meant that wait() is not a static method, so you need an
object to invoke it on. See the documentation to find which class it
belongs to and then use it on a suitable object.
cheers!
Uli
--
Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932
| |
|
| Thanks for your quick answer
I have already used Thread.sleep(someMilliseconds), and it actually worked.
I wanted to use wait() instead, because is suits my need more than sleep().
My misunderstanding was about sleep and wait, which in my mind were of the
same kind.
Using someting like My_main_class.class.wait() compiles and runs OK.
As you can guess, I'm rather a beginner in Java.
Thanks again
Denis
| |
| Gordon Beaton 2007-11-21, 8:13 am |
| On Wed, 21 Nov 2007 11:20:37 +0100, Snide wrote:
> I have already used Thread.sleep(someMilliseconds), and it actually
> worked. I wanted to use wait() instead, because is suits my need
> more than sleep().
What is your need? In what way does wait() suit your need better than
sleep()?
> My misunderstanding was about sleep and wait, which in my mind were
> of the same kind.
>
> Using someting like My_main_class.class.wait() compiles and runs OK.
Does you call My_main_class.class.notify() somewhere else in your
code? If not, then you don't need wait() at all, and I don't
understand your desire to use wait() instead of sleep().
/gordon
--
| |
| Andrew Thompson 2007-11-21, 8:13 am |
| Ulrich Eckhardt wrote:
>[quoted text clipped - 3 lines]
>
>Always use cut'n'paste for error messages!
Blood oath!* Be snide if you want, but please don't waste our
bandwidth (or your time) by being vague, or paraphrasing
compiler messages.
* Australian translation to standard English 'Too right - agree
completely'.
--
Andrew Thompson
http://www.physci.org/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.as...-setup/200711/1
| |
| Michael Redlich 2007-11-21, 7:17 pm |
| On Nov 21, 5:20 am, "Snide" <nos...@work.com> wrote:
>
> Using someting like My_main_class.class.wait() compiles and runs OK.
>
> As you can guess, I'm rather a beginner in Java.
>
Hi Snide:
As some of the other folks have already mentioned, main() needs to be
static. Here's a quick example of how to call non-static methods from
withing main():
public class HelloWorld {
public static void main(String[] args) {
// example 1
new HelloWorld().sayHello();
// example 2
HelloWorld hello = new HelloWorld();
hello.sayHello()
}
public void sayHello() {
System.out.println("Hello World!");
}
}
The first example in main() creates an instance of the HelloWorld
class without assigning the resulting object to a variable and calls
the sayHello() method. This is useful for, say, a single call like
this.
The second example in main() does essentially the same thing except
that now you have a variable to make calls to any additional methods
you may write for this particular class.
Hope this helps...
Mike.
----
Michael Redlich
ACGNJ Java Users Group
http://www.javasig.org/ (best viewed in Firefox for the time being...)
| |
| Stefan Ram 2007-11-21, 7:17 pm |
| Michael Redlich <mpredli@gmail.com> writes:
>Here's a quick example of how to call non-static
>methods from withing main():
The common »Hello world!« program already is such an example:
public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println( "Hello world!" ); }}
(»println« is a non-static method.)
| |
| Mark Space 2007-11-21, 7:17 pm |
| Snide wrote:
> Hello
>
> My program was running fine until I tried to add a timer (util.Timer) and
> relevant code.
>
> I then wanted to add a wait() in the main() , but the compiler told me that
> a non static function can't be referenced from a static context (public
> static main( args...)
You got some good answers already. I'll try to paraphrase them a little.
This bit I quoted above is the important part. "Non-static function
can't be referenced from a static context." Removing the static
declarations was the wrong direction. You need to get a non-static context.
Ready? Here it is:
new SomeObject();
Yup. That's all the compiler wanted. You can new Thread() if that's
what you need. You can also new your own class.
public class Hello {
public static void main( String [] args ) {
new Hello().sayHi();
}
public void sayHi() {
System.out.println("Hi yall.");
}
}
Notice that I call a non static method ("sayHi") from a static one
("main" in this case) by giving it a context (context = an object).
This is how you will normally use main() -- create one or more objects
then call their methods to get your program going.
You can write Java programs in all static methods, but it's usually only
appropriate for short little demo programs.
| |
| Are Nybakk 2007-11-22, 8:12 am |
| Snide wrote:
> Hello
>
> My program was running fine until I tried to add a timer (util.Timer) and
> relevant code.
>
> I then wanted to add a wait() in the main() , but the compiler told me that
> a non static function can't be referenced from a static context (public
> static main( args...)
>
> Then I tried to remove static from the main() declaration, and from most
> declaration in the source, but then the program refused to run, arguing "no
> main class can't be found", although compiling with no error.
>
> (I'm using Netbeans 5.5)
> But yet the main class is defined in the properties (the program ran OK
> before), and still appears in the properties / run / main class. (but a
> browse in the main class tab option shows an empty list).
>
> questions :
>
> How to set up a main class when Netbeans can't see no one ?
>
> Why is it necessary to declare the main() as static ?
>
> How to use wait() in a static method ?
>
>
> Denis
>
>
The main method must always be static. To call non-static
members/methods, put it in a class that you instansiate an object of.
public class SomeClass {
//vars
int testVar;
//constructor
public SomeClass() {
//...
}
//a method
public void testMethod() {
//...
}
public static void main(String arguments[]) {
SomeClass test = new SomeClass();
test.testMethod();
}
}
| |
|
| Thank you for this helpful answer (unlike someone else...).
In fact when creating a new application with Netbeans, the class, main() and
an empty contructor are created.
I had added all my functions in the class, letting the constructor empty,
since the program was running without it.
The main() only has to initialize some processing, including scheduling a
timer, and then go sleep until some event in the timer will ask main() to
awake, et to reloop to process new data.
The timer function regularly fetches a parameter file from a distant server,
whose data are intructions to my program, and occasionally a request for
updating its setup. When it is the case, the main() will reloop and restart
from the begining to set up these new parameters.
That's why wait() seemed appropriate.
Sleep() works as well, and is the solution I'm using so far, but I had to
add a flag, set in the timer event code, to ask main() to process new data.
This flag is tested every time main() get out of sleep().
I will try to restructure all the stuff according to your advice and
example.
Thanks
Denis
| |
|
|
|
|
|