Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: The order of logic in my Java Code

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Location
    UK
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question The order of logic in my Java Code

    I've been a professional developer for a lot of my career.. I'm a beginner at Java but not at Coding. The problem below is just a training exercise as such but I'd like to understand whats going on. I'm just trying to build a LIST of elements. Please excuse me that I choose it to be a list of English Football teams.. that's just a detail.

    So I have defined my own class 'Team', and I'm building a List of TEAMs. I set the values of my three attributes (2 public, one private), print out some of the detail, add to the list...repeat 3 times... then print to the list

    And my list is just the last item, added 4 tines. I've experimented with using different variable names for my Team in the Main class, and that fixes it... But I would expect my instance of team to have different values each time I add it.
          Team NextTeam = new Team();
     
         List<Team> premL = new ArrayList<Team>();
     
         NextTeam.CityName = "Preston";
         NextTeam.ExtraName = "North End";
         NextTeam.setColour("Blue");
         System.out.println(NextTeam);
         premL.add(NextTeam);
     
     
     
     
         NextTeam.CityName = "Plymouth";
         NextTeam.ExtraName = "Argyle";
         NextTeam.setColour("Green and black");
     
         premL.add(NextTeam);
         System.out.println(NextTeam.toString());
     
         NextTeam.CityName = "Manchester";
         NextTeam.ExtraName = "City";
         NextTeam.setColour("Light Blue");
         premL.add(NextTeam);
     
         System.out.println(NextTeam);
     
         NextTeam.CityName = "Gillingham";
         NextTeam.ExtraName = "";
         NextTeam.setColour("Blue");
         premL.add(NextTeam);
         System.out.println(NextTeam);
     
         Team.printTeams(premL);
     
        }
    }
    OUTPUT :
    Team{CityName='Preston', ExtraName='North End'}
    Team{CityName='Plymouth', ExtraName='Argyle'}
    Team{CityName='Manchester', ExtraName='City'}
    Team{CityName='Gillingham', ExtraName=''}
    Gillingham
    Gillingham
    Gillingham
    Gillingham


    If I use a different 'variable' name for my Team, that does fix it, but that's not the point; I'm trying to understand what is going on
    (Only changed code here, the 2nd team added using a different variable name
         ExtraTeam.CityName = "Plymouth";
         ExtraTeam.ExtraName = "Argyle";
         ExtraTeam.setColour("Green and black");
     
         premL.add(ExtraTeam);
         System.out.println(ExtraTeam.toString());

    Then the output is;
    Team{CityName='Preston', ExtraName='North End'}
    Team{CityName='Plymouth', ExtraName='Argyle'}
    Team{CityName='Manchester', ExtraName='City'}
    Team{CityName='Gillingham', ExtraName=''}
    Gillingham
    Plymouth Argyle (note change)
    Gillingham
    Gillingham
    Last edited by ChrisInTheNorth; August 22nd, 2020 at 06:58 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: The order of logic in my Java Code

    I am not sure I understand the problem. Can you explain what the problem was?
    You have posted some output but have not said if anything was wrong with that output
    or if you expected different output.
    If you expected different output could you add some comments to describe what you want to be different?
    For example
    jones <<<<<<<< THIS SHOULD BE smith

    use a different 'variable' name
    Changing a variable name should not have any effect on what is printed (unless you are using Reflection which looks inside of a class).


    Note: I only see one instance of the Team object being created. It is added to the list multiple times.
    So the list has several references to instances of the same object in it.
    The contents of that object is changed several times, but at the end there is only one object with several references all pointing to that one instance.


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2020
    Location
    UK
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The order of logic in my Java Code

    Thanks for the suggestion I put [ code ][ /code ] round my actual code.

    I was expecting the output
    Preston North End
    Plymouth Argyle (note change)
    Manchester City
    Gillingham

    I accept there is only one instance... but I thought the change in the values of that instance between the .add method calls would mean that a different value is added each time. I also accept that the fault here is with me, and that my experience of using non-object oriented language is the problem here; That I think that at the point the .add method is called, the value of the instance at that point is used.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: The order of logic in my Java Code

    at the point the .add method is called, the value of the instance at that point is used.
    The add method saves a copy of the reference to the object. It does not save a copy of the contents of the object.

    If I use a different 'variable' name
    Where is the variable: ExtraTeam declared and given a value? The posted code does not show that.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Confused adding minimum order code in my cart.html. Can anyone help? Please
    By purehobby in forum Other Programming Languages
    Replies: 2
    Last Post: October 14th, 2019, 05:26 PM
  2. Java Logic
    By JoshKesner in forum Java Theory & Questions
    Replies: 6
    Last Post: November 7th, 2012, 04:26 PM
  3. Pls explain me the code logic
    By Shajith in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:41 AM
  4. Help with java logic
    By dever in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 08:41 AM
  5. Cracking password in less number of Trials using brute forcea
    By xisstar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 10th, 2009, 10:40 AM

Tags for this Thread