Java Learning Path (i)--thinking in Java

Source: Internet
Author: User

Recently in the "tingking in Java" This book study, the second chapter exercises the topic of practice encountered a problem, No enclosing instance of type test1 is accessible. Must qualify the allocation with an enclosing instance of type test1 (e.g. X.new A () where X was an instance of Test1.

The reason for the error is that the main method is static and cannot access the dynamic class you define, that is, you cannot create a reference to the class. The correct method is simple, you can add static directly before the inner class to make it a static class.

? The source code is as follows:

public class Test1 {public    static class test{    int s;    char c;} public static void Main (string[] args) {    Test t = new test ();    System.out.println ("s" +t.s);    System.out.println ("C" +t.c);}    }

  

Java Learning Path (i)--thinking in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.