Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JAVA error: non-static method cannot be referenced from a static context

Posted on 2013-11-13
6
Medium Priority
?
799 Views
Last Modified: 2013-11-14
I am new to JAVA and have the following code that is giving me the above error.  I don't know what is causing it.  Any help is greatly appreciated.

public class Chapter7Test
{
   public boolean XOR(boolean a, boolean b)
   {
      if((a && !b) || (!a && b))
      {
         return true;
      }
      if((a || b) && !(a && b))
      {
         return true;
      }
      if(( a != b))
      {
         return true;
      }
      return false;
   }
   
   public boolean DecimalRepresentation(int x)
   {
      if(x % 100 == 0 && x % 1000 != 0)
      {
         return true;
      }
      return false;
   }
   
   public boolean isLeapYear(int year)
   {
      return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
   } 
   
   public static void main(String [] args)
   {
   System.out.println(isLeapYear(2004));
   //boolean bResults=XOR(true,true);
   //System.out.print("test XOR a=true, b=true result=");
   //System.out.println(bResults);
   }
   

}

Open in new window


This is the error I am getting when I compile:

Chapter7Test.java:36: error: non-static method isLeapYear(int) cannot be referenced from a static context
   System.out.println(isLeapYear(2004));
                      ^
1 error


Any help is greatly appreciated!
0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39646418
You need to create an instance of Chapter7Test using the new operator before you can call its methods
0
 

Author Comment

by:dyarosh
ID: 39646464
Forgive my ignorance but are you saying I need to put my methods in a different class?  Basically what I am trying to do is just test my methods which I thought I could do in the Main method.  If I don't need another class can you show me how to make the declaration in the main method?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 39646525
Chapter7Test c7t = new Chapter7Test();
System.out.println(c7t.isLeapYear(2004));

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Expert Comment

by:jb1dev
ID: 39646837
Static methods are called on a class.
Instance methods (non-static methods) are called on an instance of a class.  

E.g.
public class A {
    public static void foo() { ... }
    public void baz() { ... }
}

public class EntryPoint {
    public static void main(String[] args) {
        A.foo(); // This works.
        A.baz(); // This does not work. Your compiler will complain about this.
        
        A a = new A();  // Now we have an instance of class "A" pointed to by a reference called "a". 
                                 // So now we can call instance methods on it.
        a.baz(); // This works.
    }
} 

Open in new window

0
 

Author Closing Comment

by:dyarosh
ID: 39646851
Thanks a lot!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39647560
:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question