Static "Problems"...

Hey, could someone explain to me briefly what is required to have the word "static" in its declaration (method, field, w/e)? I made a comment on the boards some where a few days back about how I always seem to have to set everything to static when I have a GUI in my program or else I get the same error over and over and was told something was wrong but I don't have a clue as to what:

non-static method poop() can not be referenced from astatic context !!!

It always seems to give me this error with methods in which I call methods outside of it's own class. Such as:

privatevoid buildGame()

{

mazeLevel.createRooms();

GUI.buildGUI();

printWelcome();

}

// And...

publicvoid printHelp()

{

GUI.textArea.append("The only purpose for this button is to\n");

GUI.textArea.append("show off my mad help skillz.\n\n");

}

My options always end up being set everything to static or... well, actually that's my only option.

I would post my entire program source, but I figure you guys don't want to have to look through the whole thing. Any who if you do need me to post the whole thing to determine what is wrong then let me know, but I hope someone out there as a clue as to why this might be happening. thanks :-\

[1699 byte] By [sharokua] at [2007-11-27 4:50:47]
# 1

"Static" means not associated with any particular object. Instance-level methods are associated with a particular object. So from a static context, such as, for example, the standard main() method, you can't invoke non-static methods.

One thing you can do is make everything static, but that's kind of missing the point of using an object-oriented programming system.

You can also create an instance of something and start using its methods. Often in main(), you create an instance of the class that main() is in. For example:

public class GUI {

public static void main(String[] argv) {

GUI gui = new GUI();

gui.start();

}

private void start() {

setup();

mainFrame().setVisible(true);

}

}

or something like that.

paulcwa at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 2

> My options always end up being set everything to

> static or... well, actually that's my only option.

Understanding the difference between static vs instance variables and classes and knowing their nature and uses are some of the basic core concepts that are required for good coding in Java (and most any other OOP language). Getting answers to questions here and there may be useful for buidling your project but not your understanding. I recommend that you get yourself a decent textbook and study this. Another good starting off place is [url=http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html]here[/url] with the java sun tutorials.

Good luck!

/Pete

petes1234a at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 3
Hmm, that's a bit more clear. Thank you. I assume that objects inside the GUI class shouldn't be static then because the are associated with the GUI?
sharokua at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 4

> Hmm, that's a bit more clear. Thank you. I assume

> that objects inside the GUI class shouldn't be static

> then because the are associated with the GUI?

I'm not sure I understand what you mean here. I have classes that have internal to the class both static and instance variables depending on the need. Same for outside of the class.

petes1234a at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 5

Objects are never static, by definition.

Methods and fields in the GUI class (as in example above) can be either static or non-static. It depends on what they need to do. The point is, once you create an instance, you can call its non-static methods, and at that method can call other non-static methods, etc.

paulcwa at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 6

>One thing you can do is make everything static, but that's kind of missing the

>point of using an object-oriented programming system.

In fact, I would recommend that the only static method you write is the main method. (And make no data static besides "constants".) Otherwise, it's too easy to fall off the o-o wagon and make more and more methods and data static.

Hippolytea at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 7
> Objects are never static, by definition.but they can be declared as a static variable within another class.... a single object instance that is shared by all instances of the class that contains this variable, correct?
petes1234a at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 8

Question, should a GUI based program be able to compile successfully even if you have no statics at all or are there certain things that are required to be static. Because for what ever reason my just doesn't compile unless most everything is static.

Maybe I'm asking the wrong question, let me try again...

Okay, so, say I do a call from one class to another class's method... IE:

public void CALLmethod()

{

Frank.methodToCall();

}

...and I get an error saying that it needs to be static; are you saying I get this error because I need to make a "Frank object" and then call the method through that object?

sharokua at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 9

There needs to be one static method called *main*. The rest can all be instances. I have been using another static method though just cuz I've been trying to do what the sun tutorials do. Here's an example:

// here is my 2nd static method

private static void createAndShowGUI()

{

// here I initiate my main GUI class

ButtonMain bt = new ButtonMain("Sudoku Grid");

}

// here's the static main method

public static void main(String[] args)

{

javax.swing.SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

// I believe that

// this allows the program to be run in a more thread-safe way

createAndShowGUI();

}

});

}

petes1234a at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 10

> Okay, so, say I do a call from one class to another

> class's method... IE:

>

> public void CALLmethod()

> {

> Frank.methodToCall();

> }

>

> ...and I get an error saying that it needs to be

> static; are you saying I get this error because I

> need to make a "Frank object" and then call the

> method through that object?

This is a huge topic that again will require you to study java from the ground up. There are many ways that classes can interact. A common way is for one class to hold a variable of another class (a "has-a" relationship) and then call the methods from the object variable. But your question really cannot be adequately answered in a few lines in this forum. You need to understand the basics of java, OOP, and this only comes from study, study, study.

petes1234a at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 11

> > Objects are never static, by definition.

>

> but they can be declared as a static variable within

> another class.... a single object instance that is

> shared by all instances of the class that contains

> this variable, correct?

Yeah...but that object still has an associated object (i.e., itself) and so it can call its own non-static methods just fine.

paulcwa at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 12

> > Objects are never static, by definition.

>

> but they can be declared as a static variable within

> another class.... a single object instance that is

> shared by all instances of the class that contains

> this variable, correct?

No.

Objects do not have the property of being static or non-static. Member variables do. Methods do. Initializers do.

No object is ever static. No object is ever non-static.

jverda at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 13

> No.

>

> Objects do not have the property of being static or

> non-static. Member variables do. Methods do.

> Initializers do.

>

> No object is ever static. No object is ever

> non-static.

jverd, thanks for answering. Is it correct in saying then that if an object is held in a static member variable of a class, the variable is what is static, not the object itself?

Sorry if I'm being dense here. I'm just trying to fully learn the concept.

petes1234a at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 14

> > No.

> >

> > Objects do not have the property of being static

> or

> > non-static. Member variables do. Methods do.

> > Initializers do.

> >

> > No object is ever static. No object is ever

> > non-static.

>

> jverd, thanks for answering. Is it correct in saying

> then that if an object is held in a static member

> variable of a class, the variable is what is

> static, not the object itself?

>

> Sorry if I'm being dense here. I'm just trying to

> fully learn the concept.

Variables do not hold objects. They hold references to objects. But yes, it's the variable that's static or non-, not the object.

class Foo {

static Bar bar1 = new Bar();

Bar bar2 = Foo.bar1;

}

...

Foo foo1 = new Foo();

Foo foo2 = new Foo();

There is one Bar object. The object is neither static nor non-static.

There is one static reference-to-Bar variable shared by the whole class, and it points to that one Bar object.

There is one non-static reference-to-Bar variable for each Foo object created--two of them, in our case--and they all point to that one Bar object.

jverda at 2007-7-12 10:04:14 > top of Java-index,Java Essentials,New To Java...
# 15
> Variables do not hold objects. They hold references> to objects. But yes, it's the variable that's static> or non-, not the object.> .....Thank you. It is clearer to me now./Pete
petes1234a at 2007-7-21 21:14:16 > top of Java-index,Java Essentials,New To Java...