Solved

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

Posted on 2013-11-13
6
714 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 500 total points
ID: 39646525
Chapter7Test c7t = new Chapter7Test();
System.out.println(c7t.isLeapYear(2004));

Open in new window

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

738 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