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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/How to fix “no enclosing instance of type is accessible”.
Next
Answered
Teddy Brown
  • 18
Teddy Brown
Asked: May 18, 20222022-05-18T19:39:46+00:00 2022-05-18T19:39:46+00:00In: java

How to fix “no enclosing instance of type is accessible”.

  • 18

. Advertisement .

..3..

. Advertisement .

..4..

I’m trying to run a new project. I do a couple of things like this:

class Hello {
  class Thing {
  public int size;
 
  Thing() {
  size = 0;
  }
  }
 
  public static void main(String[] args) {
  Thing thing1 = new Thing();
  System.out.println("Hello, World!");
  }
 }

But in my program, I am getting the warning:

No enclosing instance of type Hello is accessible

Can someone explain why the “no enclosing instance of type is accessible” issue happened? Where have I gone wrong? Thank you!

no enclosing instance of type
  • 2 2 Answers
  • 124 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-25T03:22:49+00:00Added an answer on June 25, 2022 at 3:22 am

    The cause:

    From your program I found that the class of Thing was declared as a non-static inner class. This implies that it must be connected to a Hello class instance. You are attempting to build a Thing instance from a static context in your code. However, static methods did not have permission to access to a non-static inner class. This is the reason for your mistake.

    Solution:

    static class Thing can solve your problem and help your program run well. A specific Hello instance must be in scope for new Thing() to work if you have specified Thing as an inner class, which is linked to a specific instance of  Hello by definition (even if you never use or refer to it). If you declare it as a static type rather than a dynamic class, it will become “nested”, and you do not need a Hello instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Nathan Monnier
    2022-05-25T20:56:17+00:00Added an answer on May 25, 2022 at 8:56 pm

    The class Thing has been declared as an inner non-static class. This means that it must be associated to an instance of Hello class.

    You are trying to create a Thing instance from a static context. This is what the compiler is complaining.

    There are many possible solutions. Depending on your goals, there are a few options.

    • Move Thing from the Hello classification.

    • Thing should be changed to static nested classes.

      static class Thing
    • Before creating an instance Hello, create .

      public static void main(String[] args)
      {
       Hello h = new Hello();
       Thing thing1 = h.new Thing(); // hope this syntax is right, typing on the fly :P
      }

    If Thing relied on Hello for meaning, the last solution (a nonstatic nest class) would be mandatory. If we had:

    public class Hello {
     public int enormous;
    
     public Hello(int n) {
     enormous = n;
     }
    
     public class Thing {
     public int size;
    
     public Thing(int m) {
     if (m > enormous)
     size = enormous;
     else
     size = m;
     }
     }
     ...
    }

    Any attempt to create an object from class Thing raw, such as:

    Thing t = new Thing(31);

    It would be difficult to test 31 against it, as there isn’t an obvious enormous value. To provide this h.enormous value, an instance h from the Hello outer classes is required:

    ...
    Hello h = new Hello(30);
    ...
    Thing t = h.new Thing(31);
    ...

    It doesn’t necessarily mean a Thing if it does not have a Hello.

    For more information about inner/nested classes, see Nested Class (The Java Tutorials).

    • 13
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.