non-static method cannot be referenced from a static context
I explained this somewhat in your other thread.
The problem here is you're not understanding the most important, foundational concept in OOP. And that's the difference between a class and an object.
A class is a definition of something, and an object is an instance of something. Classes can only use class methods, and not instance methods. meanwhile, objects can use both class methods and instance methods (but in my opinion should stick with using only instance methods).
For example, let's say we have this class and these objects:
class Human: the definition of a certain species of bipedal organism which has high intelligence and refined motor skills.
objects Mary, Jane, and Watson: each of these three objects is an instance of Human. Each of these objects are created from the definition Human.
These two types are not interchangeable - neither in real life nor in Java. There are going to be methods which can only be carried out by the Human definition (or the "entire Human race") and there are methods which can only be carried out by individual humans. This is seen in the following example code.
public class Human { //1 - class variables (also known as static variables) static double averageHeight = 177.8; static double averageWeight = 141.53; static double maxRunningSpeed = 44.2; static long populationCount = 7000000000L; //2 - instance variables double height; double weight; double runningSpeed; String name; public Human(String name, double height, double weight, double runningSpeed) { this.name = name; this.height = height; this.weight = weight; this.runningSpeed = runningSpeed; } //3 - instance method public void run() { System.out.println(name + " is running at " + runningSpeed + " km/hr."); } //4 - class method (also known as static method) public static void goThroughExtinction() { populationCount = 0; System.out.println("Oh noes! It appears that the human race is no more."); } } public class Main { public static void main(String[] args) { Human Mary = new Human("Mary", 163, 156, 13); Human Jane = new Human("Jane", 166, 141, 19); Human Watson = new Human("Watson", 172, 185, 32); Human.run(); //5 - Static context error Watson.run(); Human.goThroughExtinction(); Mary.goThroughExtinction(); //6 - no error, but avoid this } }
1: Class variables apply to not any one object in particular, but all objects. In this case, the average height, weight, and top running speed apply to the entire Human species.
2: Instance variables apply to a single object and do not necessarily represent the class. In this case, a single person's height and weight doesn't represent the height and weight of the entire Human species.
3 and 5: An instance method is an action which can only be carried out by an individual object, not a class. You can say that a single person such as Watson ran at a speed of 32 km/hr, but it doesn't make sense to say "the entire human species ran at a speed of 32 km/hr." The entire human species isn't a thing that can run; only members of the human species can run. That's why you're getting a static context error. You're trying to make a class execute an instance method like I'm trying to make the human species run, and that isn't possible.
4 and 6. A class method is an action which should only be carried out by the class, not the object. The entire human species can go through extinction and die off, but it doesn't make sense to say "Mary went through extinction." A single person can't go "extinct." Despite this, Java allows you to use an object to execute a class method. So even though it doesn't make sense to say that Mary went through extinction, Java allows you to do it.
-------------------------------------------------------------------------------
So how does this apply to you? Well, first of all, what I just explained is the entire point of Object-Oriented Programming (OOP) languages such as Java. If you don't completely understand this concept, you won't be progressing very far in Java.
Second of all, points 3 and 5 explain the error you described in your original post. If you consider that
1. classes can't execute instance methods
2. playGame is a class
3. #startNewGame is an instance method
it follows logically that playGame can't execute #startNewGame. That line of code in your original post won't work. Instead, you have to create a playGame, like this:
playGame game = new playGame(); game.startNewGame();
In this case game is an object. Since instance methods are meant to be used with objects, you won't get an error here.
In my opinion, though your overall class/method structure is convoluted and doesn't make sense OOP-wise. A class should be a definition, a "class" of objects, while a method should be an action, a thing you can do. Your structure does not follow this principle because one of your classes is named "play game," which is an action instead of a definition.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now