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
Solved

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

Posted on 2013-11-13
6
659 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
windows explorer path to command prompt 5 54
servlet and mdb, jms error 1 33
Java basic valueOf question 1 29
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

856 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