Hi all,
I'm having some trouble with the static void main concept in stand
alone java applications.
Here is an example to explain my problem.
-------------8<---------------
public class Test
{
int dummy;
public void do_it()
{
}
// ******* main
public static void main (String args[])
{
dummy=1; // doesn't work
do_it(); // doesn't work
-------------8<---------------
From what i've seen so far it's not possible to use any kind of
dynamic variable or function/procedure from the base class from within
the main procedure (see example). Is this assumption correct? Or are
there ways to make this work?
Best regards,
Rick
SPG - 28 Dec 2004 03:22 GMT
From a main function, you must either call static methods and variables, or
have an instance of your object to work with, so..
public class Test
{
int dummy;
public void do_it()
{
}
// ******* main
public static void main (String args[])
{
Test test = new Test()
test.dummy=1;
test.do_it();
}
will work... as will...
public class Test
{
static int dummy;
public static void do_it()
{
}
// ******* main
public static void main (String args[])
{
dummy=1;
do_it();
}
but in an entirely different way!!!
Steve
> Hi all,
>
[quoted text clipped - 29 lines]
> Best regards,
> Rick
Andrew McDonagh - 28 Dec 2004 08:38 GMT
> Hi all,
>
[quoted text clipped - 29 lines]
> Best regards,
> Rick
Correct. Basically main(...) is like any other static method of a class
(albeit a special one that the Java runtime system knows how to run
explicitly).
Static methods can use static variables of the class or local variables
of itself.
jhsolorz@hotmail.com - 28 Dec 2004 13:21 GMT
> Hi all,
>
[quoted text clipped - 19 lines]
> dummy=1; // doesn't work
> do_it(); // doesn't work
That's because main is a static method, dummy
is an instance varaible, and do_it is an instance
method. Try (new Test()).dummy.
Jose Solorzano
newB - 28 Dec 2004 21:24 GMT
Try this out
public class Test
{
static int dummy;
static public void do_it()
{
}
// ******* main
public static void main (String args[])
{
dummy=1; // doesn't work
do_it(); // doesn't work
Chris Smith - 28 Dec 2004 21:33 GMT
[Code reformatted to fit better]
> Try this out
>
[quoted text clipped - 8 lines]
> dummy=1; // doesn't work
> do_it(); // doesn't work
Well, if you add the missing two close braces which weren't in your
original post, then yes it does work. What did you do to get it to
fail? There is some other problem here -- possibly that you're
accidentally compiling different code than you think you are.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
jeffc - 28 Dec 2004 21:48 GMT
> [Code reformatted to fit better]
>
[quoted text clipped - 15 lines]
> fail? There is some other problem here -- possibly that you're
> accidentally compiling different code than you think you are.
Or you're accidentally looking at the wrong code :-) The OP didn't write that.
Chris Smith - 28 Dec 2004 22:41 GMT
> Or you're accidentally looking at the wrong code :-) The OP didn't write that.
I was responding to "newB", who did write that (with different
formatting). If that doesn't work for newB, then newB is doing
something wrong.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
jeffc - 29 Dec 2004 16:02 GMT
> > Or you're accidentally looking at the wrong code :-) The OP didn't write that.
>
> I was responding to "newB", who did write that (with different
> formatting). If that doesn't work for newB, then newB is doing
> something wrong.
Of course it works for newB. newB wrote that code for the OP. The OP is the
one with the problem.
Chris Smith - 29 Dec 2004 16:25 GMT
> Of course it works for newB. newB wrote that code for the OP. The OP is the
> one with the problem.
Okay. I was confused because the comment "// doesn't work" was left in
the code.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation