-3

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().

  • That link is a much more complicated case I do not understand. This case is very simple, different. – user4892642 May 12 '15 at 17:56
  • read about statics and difference between normal class variables then you will get it. – cerkiewny May 12 '15 at 17:57
  • I doubt that it's the variable that's acting up in this line. – Aify May 12 '15 at 17:57
  • I take that back, it works now. That is very strange, why do I have to make another object of class C()? I should be able to call G() from "inside" since I am already in main no? – user4892642 May 12 '15 at 17:58
  • @aify true, but if you get difference between the variables then you get it with the functions. – cerkiewny May 12 '15 at 18:00

2 Answers2

0

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • How do I know if I should define a function as static? – user4892642 May 12 '15 at 17:59
  • Both approaches worked. I changed G to static and it worked as expected. I also tried removing static and using new C(), that worked too. – user4892642 May 12 '15 at 18:00
  • General rule of thumb - if the class has data members that belong to an instance (i.e., the class is statefull), and the method relates to that state - it should not be static. Otherwise, making it static is probably acceptable. – Mureinik May 12 '15 at 18:01
0

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.

Aify
  • 3,543
  • 3
  • 24
  • 43
  • What is meaning of static? Not changing? – user4892642 May 12 '15 at 18:01
  • It belongs to the class, not the objects of that class. – Aify May 12 '15 at 18:01
  • It's more like... if I have something that is the same for all objects of this class, it should probably be static. Eg: All humans sleep. If I were writing a Human class, and i had a `boolean humansSleep` it should probably be static and set to true. When creating Human objects, they'd probably have nonstatic things such as hair color and height, since those stats aren't the same for all humans. – Aify May 12 '15 at 18:05