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: What is the difference in declaring class variables?

  1. #1
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default What is the difference in declaring class variables?

    Hello and thank you for coming and reading my post. I had a question regarding class variables and constructors. I'll post some code so I can show what I mean:

    Case 1.)
    public class Shape {
     
    	private int length;
    	private int width;
    	private int height;
     
    	public Shape(int shapeLength, int shapeWidth, int shapeHeight) {
     
    		this.length = shapeLength;
    		this.width = shapeWidth;
    		this.height = shapeHeight;
     
     
    		// Test to make sure I have no error and embarrass myself on the forums :)
    		System.out.println(this.length + ", " + this.width + ", " + this.height);
    	}
    }

    Case 2.)
    public class Shape {
     
    	private static int length;
    	private static int width;
    	private static int height;
     
    	public Shape(int shapeLength, int shapeWidth, int shapeHeight) {
     
    		Shape.length = shapeLength;
    		Shape.width = shapeWidth;
    		Shape.height = shapeHeight;
     
     
    		// Test to make sure I have no error and embarrass myself on the forums :)
    		System.out.println(Shape.length + ", " + Shape.width + ", " + Shape.height);
    	}
    }
    Both of these are then run in this class:
    public class TestShape {
    	public static void main(String[] args) {
    		Shape square = new Shape(10,10,10);
    	}
    }





    Now both styles from 1 and 2 achieve the same result. I just don't understand the difference between the two.

    It seems that the keyword (static) is the cause of the change, and when I looked up static I found: The static keyword denotes that a member variable, or method, can be accessed without requiring an object of the class to which it belongs.

    So.. Does this mean that with the keyword (this), I have to have created an object in order to access the variables? And with static I don't??

    Thanks for any help!

    This is not for an assignment, I'm just curious.
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: What is the difference in declaring class variables?

    Yes and no.

    The keyword this is used to refer to the current object. That is, it provides a way for an object to refer to itself. Therefore it kinda makes sense that an object has to be created in order to use the word this. However, in your example the use of this is not needed. If you were to remove them then the code will compile and execute. This is due to using different names for your parameters and instance variables. If they had been called the same then you will need to use this.
    public class Shape {
        private int length;
        private int width;
        private int height;
     
        public Shape(int length, int width, int height) {
            this.length = length; // assigns parameter value to instance variable
            this.width = width; // ditto
            height = height; // this does nothing as it simply assigns the parameter value back to the parameter
        }
    }
    The best example of using static is for the PI constant in the Math class. All the methods in the Math class are also static You can make use of PI and the methods without creating a Math object.
    double area = Math.PI * Math.pow(radius, 2.0);
    Both PI and the pow method are used statically.

    BTW your second example is not a good use of static. it is kinda like creating a Person object and making their name static. Which means that every Person object gets the same name. Not very useful.
    Improving the world one idiot at a time!

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

    Robertgif (September 18th, 2013)

  4. #3
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: What is the difference in declaring class variables?

    Hahahha, Thank you very much, that cleared things up quite a lot, and it also made clear to me how the Math class worked.

    Thanks so much!
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: What is the difference in declaring class variables?

    I should add that it is not necessary to use this to access the instance variables if it is not needed.
    public class Shape {
        private int length;
        private int width;
        private int height;
     
        public Shape(int length, int width, int height) {
            this.length = length; // assigns parameter value to instance variable
            this.width = width; // ditto
            height = height; // this does nothing as it simply assigns the parameter value back to the parameter
        }
     
        public int getLength() {
            return length; // this is not needed
        }
    }
    Improving the world one idiot at a time!

Similar Threads

  1. declaring field variables
    By nesthead98 in forum Object Oriented Programming
    Replies: 11
    Last Post: June 15th, 2012, 02:48 PM
  2. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  3. <..> when declaring a class
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 08:47 AM
  4. Declaring variables in constructor and compiling
    By Newoor in forum Object Oriented Programming
    Replies: 3
    Last Post: December 5th, 2009, 12:07 PM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM

Tags for this Thread