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

Thread: Problem in Class, Displaying only one last change in value!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem in Class, Displaying only one last change in value!

    Okay so I have a rectangle class having the default values for the rectangle'd height, width and color and area. Everything is working fine when I test them in my Test Class except the color.
    Suppose I create two rectangle objects in test class, r1 and r2, I can find the area with different values easily and print them out, but for color, its only showing me the color of r2, If I remove object r2 and keep r1 only then I can see the color name of my object r1; otherwise its showing me the color of r2 in both r1 and r2 when I print them out. Code and output is below

    Rectangle Class
    public class Rectangle {
    private double width = 1;
    private double height = 1;
    private static String color = "white";
     
     
                    public Rectangle (double newWidth, double newHeight, String newColor ) {
                    setWidth(newWidth);
                    setHeight(newHeight);
                    setColor(newColor);
                    }
     
                    public double getWidth ( ) { 
                    return width;  
                    }  
     
                    public void setWidth ( double newWidth ) {
                      if (newWidth <= 0) {
                           System.out.println("Fatal error");
                           System.exit(0);
                      }
                      else {
                         width = newWidth;
                      }
                     }
     
     
                    public double getHeight ( ) { 
                     return height;
                                }       
     
     
                    public void setHeight ( double newHeight ) { 
                          if (newHeight <= 0) {
                            System.out.println("Fatal error");
                            System.exit(0);
                           }
                           else {
                            height = newHeight;
                          }
                    }
     
                   public String getColor ( ) {
                    return color;
                    }
     
                   public void setColor ( String newColor ) {
                     color = newColor;
                    }
     
     
                    public double findArea (  ) {
                    double area = width * height;  
     
                    return area;
                    }
                    public String toString ( ) { 
                    return ("Deafult height " + height + "Default width " + width + "Default color " + color);
                    }
     
    }


    Test Class
    public class TestClass {
     
    public static void main(String[] args){
                //invoking parameterized constructor
                Rectangle r1 = new Rectangle(7, 6, "red");        
                double area1 = r1.findArea();
     
     
                Rectangle r2 = new Rectangle(9, 5, "yellow");
                double area2 = r2.findArea();
     
     
                //printing properties of the rectangles r1 and r2
                System.out.println("Properties of the First Rectangle \'r1\' " + "\n"+ "Color: " 
                        + r1.getColor() + "\n" + "Height: " + r1.getWidth() + "\n" +
                        "Width " + r1.getHeight() + "\n" + "Area: " + area1 + "\n");
              System.out.println("Properties of the Second Rectangle \'r2\' " + "\n"+ "Color: " 
                        + r2.getColor() + "\n" + "Height: " + r2.getWidth() + "\n" +
                        "Width " + r2.getHeight() + "\n" + "Area: " + area2 + "\n");
                }
     
                }

    Output
    Properties of the First Rectangle 'r1' 
    Color: yellow
    Height: 7.0
    Width 6.0
    Area: 42.0
     
    Properties of the Second Rectangle 'r2' 
    Color: yellow
    Height: 9.0
    Width 5.0
    Area: 45.0

    See "yellow" in both r1 and r2 where as I have assigned yellow to only r2. If I remove r2 only then I can see r1's color. Help please!


  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: Problem in Class, Displaying only one last change in value!

    You need to study what the static modifier does when used with a variable in a class.
    private static String color = "white";
    color is the only class member that has that modifier and is the only one that is not unique to each instance of the class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    adnan.alvee (March 12th, 2013)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem in Class, Displaying only one last change in value!

    Thank you soo much norm! That solved it. So static sets it to only one value change or more like the recent change in any object in other classes, right?

Similar Threads

  1. Change a JLabel value that is in another class?
    By jm24 in forum AWT / Java Swing
    Replies: 3
    Last Post: February 17th, 2012, 08:13 PM
  2. [SOLVED] Printwriter class: displaying File contents to screen with included formatting?
    By mwebb in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: November 25th, 2011, 11:38 AM
  3. problem displaying a new frame
    By yemista in forum AWT / Java Swing
    Replies: 0
    Last Post: October 18th, 2011, 02:08 PM
  4. Displaying an Image Problem
    By petur170 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 23rd, 2011, 08:23 AM
  5. Problem on displaying Celcius
    By Superteo1987 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 31st, 2011, 10:23 AM