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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:47:10+00:00 2026-05-26T20:47:10+00:00

The following program uses an inner class named Anonymous which itself extends its enclosing

  • 0

The following program uses an inner class named Anonymous which itself extends its enclosing class Main.

package name;

public class Main
{
    private final String name;

    Main(String name)
    {
        this.name = name;
    }

    private String name()
    {
        return name;
    }

    private void reproduce()
    {
        new Anonymous().printName();
    }

    private class Anonymous extends Main
    {
        public Anonymous()
        {
            super("reproduce");
        }

        public void printName()
        {
            System.out.println(name());
        }
    }

    public static void main(String[] args)
    {
       new Main("main").reproduce();
    }
}

The only statement in the main() method invokes the constructor of the outer class Main supplying a string main and just then the method reproduce() is being called.


The reproduce method contains the statement new Anonymous().printName(); which invokes the printName() method on the Anonymous class object. The super(); constructor is supplying a new string reproduce to its enclosing super class Main.


Accordingly, the statement within the printName method System.out.println(name()); should display the string reproduce rather than main but it always displays the string main. Why is it so?

  • 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-05-26T20:47:10+00:00Added an answer on May 26, 2026 at 8:47 pm

    Because you’ve declared Main.name() as private, so it’s not visible as a superclass method. It is, however, visible as a method of Anonymous‘s enclosing class, so it is invoked on the enclosing object.

    So if you declare Main.name() as public or protected, you will indeed see "reproduce".
    Alternatively, if you declare Anonymous as static, it no longer compiles.

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

Sidebar

Related Questions

The following simple program in Java uses the java.util.Random class such that it always
The following is a minimal program which uses ReadDirectoryChangesW . The problem I am
I have a program that uses an external API which uses its own state.
I am running a C++ program which uses a class from another .cpp file.
Consider following program: static void Main (string[] args) { int i; uint ui; i
I have the following program: ~/test> cat test.cc int main() { int i =
Hi I have the following program. When I compile on the terminal gcc main.c
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
Consider the following program. It creates a set of pointer-to-ints, and uses a custom
I've written a Python program which uses comtypes/ctypes to create a shortcut and saves

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.