New to Java, trying to figure out how to resolve this issue.
boolean myBool = G(A,n,m,0);
For some reason it isn't like this line. Why won't it let me call this simple function? Both main() and G() are part of class C().
New to Java, trying to figure out how to resolve this issue.
boolean myBool = G(A,n,m,0);
For some reason it isn't like this line. Why won't it let me call this simple function? Both main() and G() are part of class C().
A non static method belongs to a specific instance of a class, while a static method belongs to the class itself. Inside main
, which is a static method, you cannot reference non-static methods without having a specific object to run them. E.g.:
boolean myBool = new C().G(A,n,m,0);
However, if the class has no interesting state, or it's state does not effect the method G
, you should define G
as static
.
It's likely because you didn't include static
in the definition of the G()
method.
Main()
is a static method, and since static
things run before non static things do, static things can only call/use static
things.
Note that your Main()
doesn't require you to make a C
object. It's the entry point to the program, and it doesn't make sense if you have to first make an object in order to run your program - where would you make that object from?
If you want to make non-static calls, create objects of the corresponding class.