8 Replies - 17185 Views - Last Post: 20 September 2010 - 04:11 AM Rate Topic: -----

#1 akash_9105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-April 08

Java Get and Set methods, when to use it ? classes and objects

Posted 19 September 2010 - 10:41 PM

Hey guyz, am new to java programming. its my first week. Seems that am unable to properly understand the lecture notes for classes and objects.

for example:

Create a class named Shirt with data fields for collar size and sleeve length.
Include a constructor that takes arguments for each field. Also include a String
class variable named variable named material and initialize it to “cotton”. Write
a program TestShirt to instantiate 3 Shirt objects with different collar sizes and
sleeves lengths, and then display all the data, including material, for each shirt.


well, here is what i have done.

class Shirt{
	private double collarSize;
	private double sleeveLength;
	private String material;
	
Shirt(double c,double s, String m){
	collarSize=c;
	sleeveLength=s;
	material=m;
	m="Cotton";
	
}

	void Printf(){
		System.out.println("Collarsize = "+collarSize+"   "+"SleeveLength = "+sleeveLength+"   "+"Material = "+material);
		
	}


}
public class TestShirt {
	
	public static void main(String []args){
		Shirt testShirt1= new Shirt(15,30,"cotton");
		Shirt testShirt2= new Shirt(13,28,"cotton");
		Shirt testShirt3= new Shirt(17,32,"sock");
		
		testShirt1.Printf();
		testShirt2.Printf();
		testShirt3.Printf();

		
		
	}

}






UPDATE: i guess am missing a class for Material.


Well do i need to use get and set methods ??? this is my question, could someone give me some pointers on WHEN and how to use the get set methods?


Thanx..

Is This A Good Question/Topic? 0
  • +

Replies To: Java Get and Set methods, when to use it ? classes and objects

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Java Get and Set methods, when to use it ? classes and objects

Posted 19 September 2010 - 10:53 PM

Get/set methods are used to provide read/write access to attributes of a class. This practice follows the data hiding part of encapsulation. And as your variables are private, they won't be accessible without these methods.

In fact, they are pretty easy to define. Basically, a getter method should return the variable, and the setter method should allow you to modify it.
class A{
   int x;

   int getX(){return x;}
   void setX(int x){this.x = x;}

}



Also, this line m="Cotton"; in your constructor is useless, as at this point, material has already been assigned the previous value for m.
Was This Post Helpful? 1
  • +
  • -

#3 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Java Get and Set methods, when to use it ? classes and objects

Posted 19 September 2010 - 10:53 PM

Ok well first of all remove m="Cotton"; as it is not doing anything important...
Next if your going to be implementing get methods you should be doing them like so:
class Shirt{
    private double collarSize;
    private double sleeveLength;
    private String material;
    
Shirt(double c,double s, String m){
    collarSize=c;
    sleeveLength=s;
    material=m;
    m="Cotton";
    
}
public double getCollarSize()
{
    return collarSize;
}
}



As you can see the method getCollarSize() will return the value stored in the variable called collarSize. That is how you implement get methods. For set methods you do like so ( in the Shirt class):
public void setCollarSize(double size)
{
    collarSize = size;
}


the setter method just sets the variable collarSize to the new value that we are passing to it.
Edit: Damn it macosxnerd101 ya beat me to it :D.

This post has been edited by H3R3T1C: 19 September 2010 - 10:56 PM

Was This Post Helpful? 1
  • +
  • -

#4 akash_9105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-April 08

Re: Java Get and Set methods, when to use it ? classes and objects

Posted 19 September 2010 - 11:13 PM

View Postmacosxnerd101, on 20 September 2010 - 07:53 AM, said:

And as your variables are private, they won't be accessible without these methods.


My variables are private, i have not used the set method but still it works. Am i missing something here ??


EDIT: Oops i guess i got it .. its cause am only using the printf method, that's why i don't need it ?

But

Shirt testShirt1= new Shirt(15,30,"cotton");


does not this needs a set method ? thanx

This post has been edited by akash_9105: 19 September 2010 - 11:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 ProdicalProgrammer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 26-June 09

Re: Java Get and Set methods, when to use it ? classes and objects

Posted 19 September 2010 - 11:17 PM

Nice topic!!!!!!!

@macosxonerd the second class(with getter and setter()) that aksah is trying to declare can we call that a holder class or middle class?
Was This Post Helpful? 0
  • +
  • -

#6 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Java Get and Set methods, when to use it ? classes and objects

Posted 19 September 2010 - 11:19 PM

View Postakash_9105, on 20 September 2010 - 05:13 AM, said:

View Postmacosxnerd101, on 20 September 2010 - 07:53 AM, said:

And as your variables are private, they won't be accessible without these methods.


My variables are private, i have not used the set method but still it works. Am i missing something here ??

The reason your variable are still "working" is because your void Printf() method is part of your Shirt class!
What you should be doing is something like so:

public class TestShirt {
    
    public static void main(String []args){
        Shirt testShirt1= new Shirt(15,30,"cotton");
        Shirt testShirt2= new Shirt(13,28,"cotton");
        Shirt testShirt3= new Shirt(17,32,"sock");
        System.out.println("Collar Size:" + testShirt1.getCollarSize()+...<rest of the stuff from your printf mehod...>

    }

}


As you can see im using the getter to get the collar size.

Edit: you dont need to use your set method in that case because your setting your variables in the constructor! But if you want to change the value in those variables then you need to use the setter to do so!

This post has been edited by H3R3T1C: 19 September 2010 - 11:22 PM

Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Java Get and Set methods, when to use it ? classes and objects

Posted 19 September 2010 - 11:19 PM

Because the constructor is assigning the variables the values you pass. As the constructor is within the class, it has access to those private variables. Essentially, it is setting the variables when the Object is instantiated.
Was This Post Helpful? 1
  • +
  • -

#8 akash_9105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-April 08

Re: Java Get and Set methods, when to use it ? classes and objects

Posted 19 September 2010 - 11:35 PM

Shirt testShirt1= new Shirt(15,30,"cotton");


If i want to use the setter method to set the variables the following variables 15,30,cotton ?
is it possible?

Well which one is the correct way of working with classes and objects?

my first post or using the getter/setter methods?

thanx for all the help guyz
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Java Get and Set methods, when to use it ? classes and objects

Posted 20 September 2010 - 04:11 AM

Both. If you want to access or modify variables after you instantiate your Object, you will need getter and setter methods. However, you are not required to grant such access if you don't want to. This goes to encapsulation.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1