Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:32:43+00:00 2026-06-15T04:32:43+00:00

//This is a small code to study how collection works but when I create

  • 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”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:32:44+00:00Added an answer on June 15, 2026 at 4:32 am

    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:

    • Java: Static vs non static inner class
    • Java inner class and static nested class
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is small code. But I couldn't find whats wrong with it. In my
Using this code to zip a folder and it works perfect on small files
I use this small code within a jquery function, it works in every browser
I've been working on this small piece of code that seems trivial but still,
I have this small piece of code I got from a friend, but i
I wrote this small piece of code to test something: #include <stdio.h> int main()
I have this small code that takes an object and convert it to a
I've found this small code that sends email to gmail users. I'd like the
I have this small piece of code that basically takes a list and runs
Work on this small test application to learn threading/locking. I have the following code,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.