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

  • SEARCH
  • Home
  • 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 8683825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:08:39+00:00 2026-06-12T22:08:39+00:00

In the following code snippet, presumably it appears that it should issue some compilation

  • 0

In the following code snippet, presumably it appears that it should issue some compilation error but it doesn’t:

class Outer {
    public static class Inner {
        static String obj = "Inner";
    }

    static Optional Inner = new Optional();
    //The (inner) class name and the object name are same.
}

class Optional {
    String obj = "Optional";
}

public class Main {

    public static void main(String[] args) {
        System.out.println(Outer.Inner.obj);
        //Refers to the string inside the optional class
    }
}

The class Outer has a static class inside it named Inner. Additionally, it declares an object (static) of the class Optional (static Optional Inner = new Optional();)

This object and the class names (inside the class Outer) are same which is Inner. The program displays Optional. The only expression Outer.Inner.obj within main() is expected to display Inner but it doesn’t. The actual output is however Optional which is the case of the Optional class.

One way to display Inner is by changing the object name to something else.

static Optional Inner1 = new Optional();

From the output it displays, it appears that the object name (or a variable) is chosen over a type name (the class Inner) because they have the same name. What exact case is applied here?

  • 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-12T22:08:40+00:00Added an answer on June 12, 2026 at 10:08 pm

    Paragraph 6.4.2 of the Java Language Specification has some information about the rules that apply in this case.

    A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

    This refers to paragraph 6.5 Determining the Meaning of a Name, which explains the rules in detail.

    In your example, Outer.Inner could refer to the type of the nested class named Inner, or the static member variable Inner. The rules say the variable will be chosen over the type.

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

Sidebar

Related Questions

Why does following code snippet doesn't work? It doesn't gives any error, but also
The following code snippet is causing a compilation error that I am unable to
The following code snippet is causing a compilation error that I am hard to
In the following code snippet, Foo1 is a class that increments a counter every
This following code snippet works fine using jQuery1.2.3, but it doesn’t work with latest
The following code snippet generates some warning messages when compiling: Cluster& Myclass::getCluster(const Point &p)
The following code snippet works fine in 1.8.7 on Mac OS X, but not
The following code snippet in a Django template (v 1.1) doesn't work. {{ item.vendors.all.0
Consider the following code snippet below. class X { public String toString() { return
Please look through the following code snippet - HTML <div class=aclass> <h1>This is heading

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.