0

//This is a small code to study how collection works but when I create an Mountain object in main method it gives an error. Explained further in the end.

import java. util. * ;
    public class sortMountains {
     class Mountain {
               String name;
               int height;
               Mountain(String n, int h) {
                 name      = n;

                 height = h;
               }
               public String toString( ) {
                 return name +  + height;
               }
            }
    List<Mountain> mtn  = new ArrayList<Mountain> ();
      class NameCompare implements Comparator <Mountain> {
        public int compare(Mountain one, Mountain two) {
          return one.name. compareTo(two. name);
      }
      }
      class HeightCompare implements Comparator <Mountain> {
        public int compare(Mountain one, Mountain two) {
    return (two. height - one. height) ;
    }

      }
      public void go() {
            mtn.add(new Mountain("Longs ", 14255));
            mtn.add(new Mountain("Elbert ", 14433));
            mtn.add(new Mountain("Maroon " , 14156));
            mtn.add(new Mountain("Castle ", 14265));

            System.out.println("as entered:\n" + mtn);
            NameCompare nc = new NameCompare();
            Collections.sort(mtn, nc);
            System.out.println("by name:\n'" + mtn);
            HeightCompare hc = new HeightCompare();
            Collections.sort(mtn, hc);
            System.out.println("by height:\n " + mtn);
          }
    public static void main(String args[]){

        sortMountains   sorting=new sortMountains();
        sorting.go();
         //error line   Mountain a=new Mountain("Everest",12121);            
    }

    }

The above when compiled without error line works fine but when I want to create an object of Mountain in main method it gives an error "non-static cannot be referenced from static method"

Nick Div
  • 5,338
  • 12
  • 65
  • 127
  • 1
    **1.** Search for [non-static cannot be referenced from static method](http://stackoverflow.com/search?q=non-static+cannot+be+referenced+from+static+method&submit=search) **2.** Read some posts/answers **3.** Avoid -1's –  Nov 22 '12 at 07:51
  • http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static (one out of a million, it has lots of upvotes anyway) –  Nov 22 '12 at 07:53

5 Answers5

3

Make the Mountain class static, like so:

static class Mountain {
   ...

If you don't, every instance of Mountain has to have an instance of sortMountains associated with it. Since there's no such instance in main() (the method itself is static), the compiler won't allow you to instantiate Mountain there.

For further discussion, see:

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

Inner classes are just like any other members of a class. So you cannot refer a non-static inner class in main() method, it is just like you cannot refer a non-static global variable in main() method. So you can declare your class as static to avoid that error:

static class Mountain { ...
Juvanis
  • 25,802
  • 5
  • 69
  • 87
1

Mountain class is inner class, so create mountain object as :

sortMountains sorting=new sortMountains();
Mountain a = sorting.new Mountain("Everest",12121);

For your program you can not do this because Mountain class is non-static and you can not access non-static members from static methods.

So you can declare Mountain class as static and create it's object as Mountain a = new Mountain("Everest",12121);

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
1

As you can see, Mountain class it cannot be created without its enclosing class sortMountains. You should declare sortMountains static inside Mountain if you want to create it in the way you want.

Ioan
  • 5,152
  • 3
  • 31
  • 50
1

You have declared the Mountain class inside sortMountains class. In that case you can access your inner class

  1. if your inner class is static (static class Mountain{}) or

  2. Creating outer instance.new innerClass().

    Mountain a=sorting.new Mountain("Everest",12121);

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103