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 5 of 5

Thread: need help with simple code pls T_T

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default need help with simple code pls T_T

    Can anybody please help explain why the output is not giving the proper result? Is this a semantic error?

     
    public class Programming1Practice {
     
     
        /* testing out instantiation and object oriented programming */
     
     
        public static class staticValues {
     
            static int num1 = 0;
            int num2;
        }
     
     
        public static void main(String[] args) {
     
            staticValues s1 = new staticValues();
            s1.num1 = 10;
            s1.num2 = 9;
     
            staticValues s2 = new staticValues();
     
            staticValues s3 = new staticValues();
            s3.num1 = 8;
            s3.num2 = 2;
     
            System.out.println("num1 = " + s1 + "num2 = " + s2);
        }
    }

    the output is:

    run:
    num1 = programming1practice.Programming1Practice$staticVa lues@2f995c9anum2 = programming1practice.Programming1Practice$staticVa lues@7d8e9adf
    BUILD SUCCESSFUL (total time: 1 second)
    Last edited by GregBrannon; October 5th, 2014 at 06:21 AM. Reason: Fixed tags: no spaces please.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: need help with simple code pls T_T

    I fixed your highlight tags. No spaces.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    What is the proper result? s1 and s2 are objects. Perhaps if you added a toString() method for the staticValues class the output would be to your liking.

    BTW - class names should begin with capital letters.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile Re: need help with simple code pls T_T

    Thank you Mr.Brannon, there is much to learn from you

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: need help with simple code pls T_T

    You're welcome. Does that mean you fixed it?

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    cosmicvista (October 7th, 2014)

  6. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: need help with simple code pls T_T

    I changed the class name to start with capital letters and turns out it didn't give the result that was shown in the video lecture because instead of
    System.out.println("num1 = " + s1.num1 + "num2 = " + s2.num2);
    in the code above i just had

    System.out.println("num1 = " + s1 + "num2 = " + s2);

Similar Threads

  1. new concept beginner struggles pls help T_T
    By cosmicvista in forum Object Oriented Programming
    Replies: 2
    Last Post: October 5th, 2014, 06:23 AM
  2. [SOLVED] What is wrong with my code? pls help!
    By nikolaiL in forum What's Wrong With My Code?
    Replies: 21
    Last Post: May 10th, 2014, 04:52 PM
  3. Simple question pls help beginner learning java
    By DroidJava in forum Java Theory & Questions
    Replies: 2
    Last Post: November 12th, 2013, 12:04 AM
  4. Replies: 1
    Last Post: November 11th, 2011, 06:46 PM
  5. Could some pls help me out with this code??
    By Pearl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 11:01 AM