Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 834
  • Last Modified:

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

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
dyarosh
Asked:
dyarosh
  • 3
  • 2
1 Solution
 
CEHJCommented:
You need to create an instance of Chapter7Test using the new operator before you can call its methods
0
 
dyaroshAuthor Commented:
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
 
CEHJCommented:
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.

 
jb1devCommented:
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
 
dyaroshAuthor Commented:
Thanks a lot!
0
 
CEHJCommented:
:)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now