Solved

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

Posted on 2013-11-13
6
529 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 62
countHi2 challenge 7 44
create a gui in perl 3 46
Java DateChooser? 2 14
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now