1

I have a few classes I need: Score, Course, and handicap. The file is "handicap.java", thus the main class is "handicap".

If I try and nest the Score class or the Course class inside of the "handicap" class, I receive this error upon trying to instantiate an instance of either class:

handicap.java:129: non-static variable this cannot be referenced from a static context
                        Score sc = new Score(score, course);
                                   ^
handicap.java:141: put(java.util.GregorianCalendar,Score) in java.util.Map<java.util.GregorianCalendar,Score> cannot be applied to (java.util.GregorianCalendar,handicap.Score)
                        g.scores.put(greg, sc);

If I add "static" to the Score class declaration, I still receive the second error. Help?

The Code is here: http://pastebin.com/CvT1SCvb

2
  • 1
    You need to show the code, not just the error messages. Commented Jan 26, 2014 at 5:03
  • the code is not complete ..lots of missing stuff .. structure it so that its easier to debug Commented Jan 26, 2014 at 5:18

2 Answers 2

1

take a look on this

public class Handicap {

    public class Score{

    }

    public static class ScoreStatic{

    }

    public static void main(String[] args) {
        Handicap h = new Handicap();
        h.method();
        new Handicap.ScoreStatic();
    }

    public void method(){
        new Handicap.Score();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

By the looks of it , you are getting compilation error.

Please read about the Nested Classes : http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

This is the way it works :

    public class handicap{
    public static void main(String... args) {
         handicap st = new handicap();
         handicap.Score fl = st.new Score();

     }

    class Score{
      //blah
     }

   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.