7 Replies - 404 Views - Last Post: 28 May 2010 - 08:43 AM Rate Topic: -----

Topic Sponsor:

#1 Guest_Default*


Reputation:

Modifying an Object's Instance Variables From Outside the Class

Posted 28 May 2010 - 12:04 AM

Hi. I don't understand what you would need to type into the group class that will let you hold the arrays.

This is what I have at the moment.

public class Group
{
	private Team[] teams;
	private int index;
	
	public Group()
	{
		Team[] groupteams = new Team[4];
		index = 0;
	}	
	
	public boolean addTeam(Team t)
	{
		if (index < 0 || index > 3)
		{
			System.out.print("Cannot add Team.");
		}
		else
		{
			teams[index] = t;
		}
		return index;
	}
	
	
} 


Is This A Good Question/Topic? 0

Replies To: Modifying an Object's Instance Variables From Outside the Class

#2 Guest_Default*


Reputation:

Re: Modifying an Object's Instance Variables From Outside the Class

Posted 28 May 2010 - 12:06 AM

Sorry. My question is how would I go about storing the information that is from the Main class into the array that is in the Group class?
Was This Post Helpful? 0

#3 seozest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-May 10

Re: Modifying an Object's Instance Variables From Outside the Class

Posted 28 May 2010 - 02:53 AM

Scanner should be used when getting input from user.
simply use add()will do for above logic.

Thanks,
Java Coder
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7493
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: Modifying an Object's Instance Variables From Outside the Class

Posted 28 May 2010 - 05:40 AM

Topic split. Please avoid necroposting to ask a new question. :)
Was This Post Helpful? 0
  • +
  • -

#5 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 59
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Modifying an Object's Instance Variables From Outside the Class

Posted 28 May 2010 - 05:59 AM

your question is a little unclear to me
but I think you want to ask how to modify the state of the object from outside the class
if this is the question
the solution is simple
here is the sample code that you can study and make changes in your code
 class SomeClass {
	int i = 10;
}

public class AnotherClass{
	public static void main(String[] args) {
		SomeClass s = new SomeClass();
		System.out.println(s.i); // here we just check whether we can access and print the value
		s.i = 20; // here we modify the values
		System.out.println(s.i);//print the modified value
	}
}



in the similar way you can apply this to your class
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1099
  • View blog
  • Posts: 2,641
  • Joined: 11-December 07

Re: Modifying an Object's Instance Variables From Outside the Class

Posted 28 May 2010 - 06:48 AM

Making the field more visible is generally a bad idea. A much better way is to use a setter method. Here is an example:

class VintageWine {

    private int age_years;

    public VintageWine(int initialAge_years) {
        setAge_years(initialAge_years);
    }

    public void setAge_years(int newAge_years) {
        age_years = newAge_years;
    }

    public int getAge_years() {
        return age_years;
    }

}


Here we can see all modifications to the age are done using the setAge_years method, even from inside the class. It seems a bit long-winded but it does have its advantages. Say we find some bugs in the application due to some wines being recorded as having negative ages, we can make this class more robust by putting some bounds checking in the setAge_years() method:

    public void setAge_years(int newAge_years) {
        if (newAge_years < 0) {throw new IllegalArgumentException();}
        age_years = newAge_years;
    }



Since this method is the only code that can modify the variable, we know that the bounds is checked even from the constructor. We can do other things like add other modification methods:

    public void setAge_decades(int newAge_decades) {
        newAge_years = 10 * newAge_decades;
        setAge_years(newAge_years );
    }



This also gives you the freedom to change the internal representation of the class (e.g. store the age in months or by the year instead of age) without having to modify the rest of the program.
Was This Post Helpful? 3
  • +
  • -

#7 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 492
  • View blog
  • Posts: 1,448
  • Joined: 19-October 09

Re: Modifying an Object's Instance Variables From Outside the Class

Posted 28 May 2010 - 07:39 AM

Nice Explination cfoley,

This is what you call Encapsulation, A key concept in OOP is Encapsulation, where access is limited to members of the same class.

In simple terms, 'information hiding'.

In the example above, the variable age_years is only Acessible to the class VintageWine.

Whats the way round it, get and set methods with a visibility of public.

You can then restrict direct access to the members of that class.
Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1099
  • View blog
  • Posts: 2,641
  • Joined: 11-December 07

Re: Modifying an Object's Instance Variables From Outside the Class

Posted 28 May 2010 - 08:43 AM

Thanks m-e-g-a-z!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1