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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:28:14+00:00 2026-06-15T09:28:14+00:00

According to the javadocs A subclass inherits all the members (fields, methods, and nested

  • 0

According to the javadocs “A subclass inherits all the members (fields, methods, and nested classes) from its superclass“. Also, Java manipulates objects by reference So why does this subclass return the wrong value for aList[0] ? It seems each class is modifying their own array when I expect them to both modify the same array.

public class mystery {
protected List<String> aList;

public mystery() {
    aList = new ArrayList<String>();
}

public void addToArray() {
    //"foo" is successfully added to the arraylist
    aList.add("foo");
}

public void printArray() {
    System.out.println( "printArray " + aList.get(0) +"" );
}
public static void main(String[] args) {
    mystery prob1 = new mystery();
    mysterySubclass prob2 = new mysterySubclass();
    //add "foo" to array
    prob1.addToArray();
    //add "bar" to array
    prob2.addToArray2();

    //expect to print "foo", works as expected
    prob1.printArray();
    //expect to print "foo", but actually prints "bar"
    prob2.printArray();
    //expect to print "foo", but actually prints "bar"
    prob2.printArray2();
}   

}

public class mysterySubclass extends mystery {

public void mysterySubclass() {}
public void addToArray2() {
    aList.add("bar");
}

public void printArray2() {
    System.out.println( "printArray2 " + super.aList.get(0) +"" );
}

}

  • 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-15T09:28:16+00:00Added an answer on June 15, 2026 at 9:28 am
        mystery prob1 = new mystery();
        mysterySubclass prob2 = new mysterySubclass();
    

    Here you are creating two new objects, so they will have their own list and wont share one as you expect.

    What you are looking for is:

        mystery prob1 = new mysterySubclass();
        mysterySubclass prob2 = (mysterySubclass)prob1;
    

    Try this..

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

Sidebar

Related Questions

According to the javadocs, Groovy's MockFor object always ends with a verify. Its StubFor
According to the Javadocs for Runtime here : Every Java application has a single
According to http://java.sun.com/products/javamail/javadocs/javax/mail/FetchProfile.html , Message[] msgs = folder.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE);
According to Serializable javadoc, readResolve() is intended for replacing an object read from the
I need to generate a MS Excel 2007+ file from my application according to
According to Javadocs, when I used javax.sound.sampledTargetDataLine 's following method: public void open(AudioFormat format,int
The JavaDocs for java.util.logging.Level state: The levels in descending order are: SEVERE (highest value)
According to the java docs of PreparedStatement.setNull: Note: You must specify the parameter's SQL
According to its JavaDoc, PersistenceAnnotationBeanPostProcessor seems to be responsible for injecting the EntityManager with
According the javadoc of Class Every array also belongs to a class that is

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.