AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static void main problem

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Rick D.
Guest





PostPosted: Tue Dec 28, 2004 2:53 am    Post subject: static void main problem Reply with 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
SPG
Guest





PostPosted: Tue Dec 28, 2004 3:22 am    Post subject: Re: static void main problem Reply with quote



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





PostPosted: Tue Dec 28, 2004 8:38 am    Post subject: Re: static void main problem Reply with quote



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





PostPosted: Tue Dec 28, 2004 1:21 pm    Post subject: Re: static void main problem Reply with quote


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





PostPosted: Tue Dec 28, 2004 9:24 pm    Post subject: Re: static void main problem Reply with 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

Back to top
Chris Smith
Guest





PostPosted: Tue Dec 28, 2004 9:33 pm    Post subject: Re: static void main problem Reply with quote

[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





PostPosted: Tue Dec 28, 2004 9:48 pm    Post subject: Re: static void main problem Reply with quote


"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 Smile The OP didn't write that.



Back to top
Chris Smith
Guest





PostPosted: Tue Dec 28, 2004 10:41 pm    Post subject: Re: static void main problem Reply with quote

jeffc <nobody (AT) nowhere (DOT) com> wrote:
Quote:
Or you're accidentally looking at the wrong code Smile 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





PostPosted: Wed Dec 29, 2004 4:02 pm    Post subject: Re: static void main problem Reply with quote


"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 Smile 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





PostPosted: Wed Dec 29, 2004 4:25 pm    Post subject: Re: static void main problem Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.