• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static main and referencing non-static stuff  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been putzing around with Java using Eclipse. (And I like what I have seen so far.) I am confused by one thing, though. Every time I reference something (either a method or a variable) in the rest of the class from the main() method, it must be static. And evertyhing I reference from within those methods must also be static. It seems as if the requirement is that everything reference by the main() function must also be static since main() is.

Below is some sample code in which the actionPerformed() method is not static and I am not sure why it doesn't have to be. I think it's because it's a method of *another* class and the requirement is for variables and methods from within the same class (since static-ness is class-related).

Is this correct?


******************************
Sample code below
******************************

public class ClientFrame extends JFrame implements ActionListener {

public void actionPerformed(ActionEvent arg0) {
System.out.println("hello!");
}

public static void main(String[] args) {
frame = new ClientFrame();
}
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

You're correct, yes. But let's see if we can't clear things up a bit more.

Static methods can only call other static methods, unless they provide an object on which that method should be invoked. Or put another way: to call a non-static method, there must be an object to call it on; if main() wants to call such a method, it has to create an object, and call the method on the object -- i.e.,



In many (most) real applications, main() looks a lot like the above.
 
Etienne LeGros
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, thanks for the welcome.

Secondly, thanks for the quickness of the response!

Thirdly, I was in the middle of writing a question about why the all-static requirement exists when I had an epiphany in the middle of formulatng my question. Funny how having to clearly ask a question can lead you to see the answer!

Again, thanks - I'm new in these here parts, but seems like people're neighborly.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Etienne LeGros:
Funny how having to clearly ask a question can lead you to see the answer!


Yes. I've come to call this the Rubber Duck effect. Happens to me all the time!
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rubber duck effect

i always found this to be the major benefit of doing code reviews with my peers. most of the time when a problem is found, it's found while the person who made the changes is explaining what the bug was/how it's supposed to work/what they did to fix it, not by the reviewer.

While i don't have a rubber duck, I often speak to my Plush Cthulhu doll that sits on my tower...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!