| View previous topic :: View next topic |
| Author |
Message |
Rick D. Guest
|
Posted: Tue Dec 28, 2004 2:53 am Post subject: static void main problem |
|
|
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
|
|
| Back to top |
|
 |
SPG Guest
|
Posted: Tue Dec 28, 2004 3:22 am Post subject: Re: static void main problem |
|
|
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
"Rick D." <no (AT) mail (DOT) com> wrote
| Quote: | 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
|
|
|
| Back to top |
|
 |
Andrew McDonagh Guest
|
Posted: Tue Dec 28, 2004 8:38 am Post subject: Re: static void main problem |
|
|
Rick D. wrote:
| Quote: | 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
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.
|
|
| Back to top |
|
 |
jhsolorz@hotmail.com Guest
|
Posted: Tue Dec 28, 2004 1:21 pm Post subject: Re: static void main problem |
|
|
Rick D. wrote:
| Quote: | 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
|
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
|
|
| Back to top |
|
 |
newB Guest
|
Posted: Tue Dec 28, 2004 9:24 pm Post subject: Re: static void main problem |
|
|
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
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Tue Dec 28, 2004 9:33 pm Post subject: Re: static void main problem |
|
|
[Code reformatted to fit better]
newB <david.c.fu (AT) gmail (DOT) com> wrote:
| Quote: | 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
|
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.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
jeffc Guest
|
Posted: Tue Dec 28, 2004 9:48 pm Post subject: Re: static void main problem |
|
|
"Chris Smith" <cdsmith (AT) twu (DOT) net> wrote
| Quote: | [Code reformatted to fit better]
newB <david.c.fu (AT) gmail (DOT) com> wrote:
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
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.
|
Or you're accidentally looking at the wrong code The OP didn't write that.
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Tue Dec 28, 2004 10:41 pm Post subject: Re: static void main problem |
|
|
jeffc <nobody (AT) nowhere (DOT) com> wrote:
| Quote: | 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.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
jeffc Guest
|
Posted: Wed Dec 29, 2004 4:02 pm Post subject: Re: static void main problem |
|
|
"Chris Smith" <cdsmith (AT) twu (DOT) net> wrote
| Quote: | jeffc <nobody (AT) nowhere (DOT) com> wrote:
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.
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Wed Dec 29, 2004 4:25 pm Post subject: Re: static void main problem |
|
|
jeffc <nobody (AT) nowhere (DOT) com> wrote:
| Quote: | 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.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
|