• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method design problem

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ops I meant class dependence instead of class independence.
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic