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 9212763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:35:58+00:00 2026-06-18T01:35:58+00:00

Possible Duplicate: non-static variable cannot be referenced from a static context (java) public class

  • 0

Possible Duplicate:
non-static variable cannot be referenced from a static context (java)

public class DemoJava {

    public class Hello {

        void fun()
        {
            System.out.println("This is static fun man!!");    
        }
    }

    public static void main(String[] args) {

        Hello hello = new Hello();
        hello.fun();
    }
}

In this example it will give me an error since I am trying to access a non-static class from a static method. Fine. For instance, if I have the same Hello class in another file and I do the same thing it does not give me an error.

Even in that case we are trying to access non-static class from static method. But that doesn’t give any error. Why?

  • 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-18T01:35:59+00:00Added an answer on June 18, 2026 at 1:35 am

    In this example it will give me an error since I am trying to access a non-static class from a static method.

    No, it will give you an error because you’re trying to create an instance of an inner class (which implicitly has a reference to an instance of the enclosing class) when you don’t have an instance of the enclosing class.

    The problem isn’t the call to fun() – it’s the constructor call.

    For example, you can fix this by using:

    DemoJava demo = new DemoJava();
    Hello hello = demo.new Hello();
    

    Or you could just make it a nested but not inner class, by changing the class declaration to:

    public static class Hello
    

    Read section 8.1.3 of the JLS for more information on inner classes, and section 15.9.2 for determining enclosing instances for a class instance creation expression:

    Otherwise, C is an inner member class (§8.5), and then:

    • If the class instance creation expression is an unqualified class instance creation expression, then:

      • If the class instance creation expression occurs in a static context, then a compile-time error occurs.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: non static method cannot be referenced from static context hey i have
Possible Duplicate: Why do I get non-static variable this cannot be referenced from a
Possible Duplicate: What is the reason behind non-static method cannot be referenced from a
Possible Duplicate: What is the reason behind “non-static method cannot be referenced from a
Possible Duplicate: What is the reason behind “non-static method cannot be referenced from a
Possible Duplicate: Java: how to call non static method from main method? I am
Possible Duplicate: Cannot refer to a non-final variable inside an inner class defined in
Possible Duplicate: Cannot refer to a non-final variable inside an inner class defined in
Possible Duplicate: Cannot refer to a non-final variable inside an inner class defined in
Possible Duplicate: static in the main class java and non static in constructor i

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.