• Post Reply Bookmark Topic Watch Topic
  • New Topic

the main method is static - how do I access other non-static methods to run my program?  RSS feed

 
J Steele
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having some trouble with the main method in my program. I know I can't access a non-static method or class from within a static method, but I'm having trouble figuring out how I resolve this so I can access and run the rest of my program (non-static) from main (static).

I've been teaching myself from the Head First Java book, and I've tried using a combination of their examples, Google, and this forum to resolve my questions, but I'm a bit stuck.

One of the sample codes in the book shows a main method that looks like this:



But when I try to set up mine in a similar way (use main to create an object, use that object to call a method that runs the rest of my program), I get the same compiling errors I do with the code below: "non-static variable this cannot be referenced from a static context" and/or "non-static method (my starter method) cannot be referenced from a static context"

My current code (trying to initialize and run the program from within main - not using the method above, because that just made it messier) is below:

 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so here are your errors:

"non-static variable this cannot be referenced from a static context" and/or "non-static method (my starter method) cannot be referenced from a static context"


What exactly does this refer to?

What is the difference between instance variables/methods and static variables/methods?

It's crucial for you to understand these concepts before you proceed further. Look at Bruce Eckel's Thinking in Java: Chapter 2 available here.






 
J Steele
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the code I copied above, the two compiling errors I referenced are:



There were also other compiling errors, but I think if I can figure out why these happened and how to fix them, I can solve the others on my own.

I glanced through the chapter you referenced, and will read it more thoroughly, but I wanted to respond first -

It sounds like I may have some additional problems with my code - I have methods that are not within classes, and are not static. From what I read, it sounds like any method or variable that does not refer to a specific named object must be declared static. Therefore, if I have a method that is not within an object/class, it needs to be static. Is this correct?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Steele wrote:For the code I copied above, the two compiling errors I referenced are:



The first one looks like you have defined a non-static nested class called Paragraph. Avoid those until you understand the basics. Move the class to the top level.

In both cases, you can't refer to a non-static member (variable, method, or nested class) without an instance to hold them. So for the second, you'd do something like this:


 
J Steele
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From looking at the examples in the book, I thought my program code was all supposed to be contained within a class - in this case, the program WordCount within public class WordCount. If I am wrong, then yes - I have nesting classes, which was completely unintentional.

In re-examining my code and the sample in the book, it looks like my first step is actually to create a new WordCount object from within the main method. Is this correct?


My program looks like this (I have removed most of the filler code, so all that is left is the classes and methods, and not what each class and method does, to simplify):

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Steele wrote:From looking at the examples in the book, I thought my program code was all supposed to be contained within a class - in this case, the program WordCount within public class WordCount. If I am wrong, then yes - I have nesting classes, which was completely unintentional.


You have at least two classes: WordCount and Paragraph. From your previous post, I'm guessing that you defined Paragraph inside of WordCount. So if you move it out to the top level--preferably to its own .java file--that problem should go away.

In re-examining my code and the sample in the book, it looks like my first step is actually to create a new WordCount object from within the main method. Is this correct?


Yes. At least, if you're going to call a non-static WordCount method, you have to create a WordCount object somewhere before you do so.

Just a bit of a digression, but related: For very simple beginner programs, often the entire program is in main(), or the main steps are, with maybe one or two calls to static methods in that same class. That's okay at first, to get an idea of what Java code looks like in general and avoid biting off too much at once, but once we get to more complex programs, main will do very little. It will generally create an instance of that class or some other class, maybe do a small bit of configuration or set-up, and then call some go()/doIt()/execute()/etc. method on that instance, which in turn will use whatever other objects it needs to do its work.

My program looks like this (I have removed most of the filler code, so all that is left is the classes and methods, and not what each class and method does, to simplify):


And are you still having a problem with it?
 
J Steele
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I have wildly simplified everything (I know I've made several mistakes so far, and the best solution I see is to take this back to step one), and here is my program now.

My goal right now is to get my code outside of main, like you suggested (and my Java book also suggests), and print one line of code to prove that everything is working.

My code is now:



It now compiles and runs. Woooo! Now to piece everything back in, one step at a time. Before I do this, can you please confirm that I have indeed solved my initial problem in this thread, and managed to move my program out of main in a way that makes sense, and won't backfire on me? I just want to make sure I'm following proper protocols for all of this before I move on.

And - thank you, all of you, for your help!

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that looks correct, in terms of creating an instance and calling its method to solve the compiler error about "non static cannot be accessed from static context."

And double snap props to you for gutting the code and starting with a small, simple example that focuses only on the concept you were having trouble with, and then reconstructing piece by piece in small, manageable steps. Keep that technique in your toolbox. It will serve you well as long as you continue to write code.

A lot of people--and I mean A LOT--write huge reams of code before compiling or testing, then when it doesn't work, they post it here, and of course nobody wants to read that much code. When we suggest they start fresh with a tiny program that only has what they need to reproduce their problem, they often resist, presumably because they feel like that's a big step backward. It's refreshing to see someone willing to do it the right way.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!