• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method design problem  RSS feed

 
Emnaki Chih
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say a method in one class needs another variable from another class. Which way of doing this would be best?
1. calling the variable/showVarible_method directly in the method body.
2. Use the static driver class to call the variable, then insert the value/reference into the method as a parameter.

Which is best in what situations and why?
 
Tim Leung
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest we should design a instance method in a class to get variable than just get class variable(especially for edit variable) There are 2 examples.

class OldA{
public int variable_A=10; // it is a class variable
}
class NewA{
private int variable_A=10; // mark it as private, so that this variable cannot be getted.
public int getVariableA(){
return variable_A;
}
}

Design an implementation class.
class TestA{
public static void main(String args[]){
int intA;
OldA oldA=new OldA(); //Create object oldA
NewA newA=new NewA(); //Create object newA
System.out.println("oldA.variable_A="+oldA.variable_A);
oldA.variable_A=11; //Change class member of oldA
System.out.println("oldA.variable_A="+oldA.variable_A);
intA=newA.getVariableA();
System.out.println("newA.variable_A="+intA); //intA=10;
intA=intA+1;
System.out.println("newA.variable_A="+intA); //intA=11;
System.out.println("newA.variable_A="+newA.getVariableA() ; //newA.getVariableA()=10, remain unchanged

}

Use newA.getVariableA() method to get variable can protect class variable prevented from careless change.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ken,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It reqiures that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. You can change your display name here.

Second, I'm not sure I understand what the alternatives are here. Maybe you could show us some example code? Be sure to use the UBB "CODE" tags so your code can be formatted (UBB tags are like HTML tags but with square brackets ([]) instead of angle brackets.)
 
Emnaki Chih
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the naming thing it is fixed now. I am sort of new to java and this board but I'd be staying here for a while to come
Below is the code for the two cases I mentioned in my first post:
1.

2.


You see they both do the same thing, only the second example has less class independence between class A and B as they are only linked in the Static Driver class. So which way is better and why?
 
Emnaki Chih
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ops I meant class dependence instead of class independence.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!