• Post Reply Bookmark Topic Watch Topic
  • New Topic

non-static method cannot be referenced from a static context in Java  RSS feed

 
Jaya Sureya
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to compile the following code where the entry point is Main.java but i am getting the following error "Error non-static method add(int[]) cannot be referenced from a static context in Java "

If i add the keyword static in add method of A.java it works properly can someone explain the concept behind this...forgive me for being a newbie :/


Class Main.java code






Class A.java code


Both the classes are in same package
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaya,
Welcome to CodeRanch! I've added [ code ] tags to make your post easier to read.

When you call A.add(a), you are saying to call a method defined on the class. That's what a static method is.

If you wanted to use an instance method (one without the word static), you'd need to create an instance of the object:



You might not have learned about instantiating an object yet, but it is coming.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!