1

I'm trying to share variables between methods in different classes, but I don't know if I'm doing this in the correct way. Basically when I wanna use the variables on method2 I have to "transport" them throught method1 to method2 from the Class A, just take a look at the example because I don't know how to explain this properly.

Is this the correct way to do it? Because sometimes I do this over an over through methods and it looks ugly.

Example:

public class A {
    int var1, var2, var3;        
    B b = new B();        
    b.method1(var1, var2, var3);
}

public class B {
    public void method1(int var1, int var2, int var3){
       //doSomething
        method2(var2, var3);
    }

    public void method2(int var2, int var3){ 
        //doSomething
    } 
}

Btw, is there any community where code reviews are done? I'm pretty new to code and I'm afraid that I'm creating code that isn't effective.

Thanks for the help! :)

Ankush soni
  • 1,439
  • 1
  • 15
  • 30
miguelfbrito
  • 51
  • 1
  • 1
  • 7
  • share them ? not. provide getters and setters, but don't make them public or directly share them. – Stultuske Aug 18 '15 at 11:40
  • 6
    Community for Code Reviews: http://codereview.stackexchange.com/ – StefanHeimberg Aug 18 '15 at 11:41
  • Your method1 and method2 in Class B can return values instead of the void return type. – Ramón J Romero y Vigil Aug 18 '15 at 11:41
  • BTW: I would recommend you right from the beginning of the following book: https://www.ebooks-it.net/ebook/clean-code – StefanHeimberg Aug 18 '15 at 11:44
  • 1
    *"I'm trying to share variables between methods in different classes"* Why? – T.J. Crowder Aug 18 '15 at 11:44
  • _Is this the correct way to do it?_ Depends what you're using them for. Passing 3 parameters to a method is too much in most cases. – Manu Aug 18 '15 at 11:44
  • @Manu - much in Java terms, C programmers would say: "3 parameters? its like nothing is passed to this function" :D – itwasntme Aug 18 '15 at 11:46
  • 3
    @Manu *Passing 3 parameters to a method is mostly too much.* why do you think that? – Tim Aug 18 '15 at 11:46
  • @mastah I'm happy I don't need to read your C code then :-) http://www.informit.com/articles/article.aspx?p=1375308 – Manu Aug 18 '15 at 12:08
  • @Manu same to you, a person which I may consider as a nice, isn't amout of parameters depends on usage context? http://stackoverflow.com/questions/174968/how-many-parameters-are-too-many – itwasntme Aug 18 '15 at 12:26
  • 1
    @T.J.Crowder That question made me understand what I'm doing wrong, damn, it was right in front of my eyes, thank you very much! – miguelfbrito Aug 18 '15 at 12:53
  • Possible duplicate of [Why use getters and setters?](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) – miguelfbrito Apr 01 '17 at 16:59

4 Answers4

2

Use getters and setters to get variable of Class A from B as following..

public class A {

    private int var1, var2, var3;

    public int getVar1(){
        return var1;
    }

    public void setVar1(int var1){
        this.var1 = var1;
    }

    public int getVar2(){
        return var2;
    }

    public void setVar2(int var2){
        this.var2 = var2;
    }

    public int getVar3(){
        return var3;
    }

    public void setVar3(int var3){
        this.var3 = var3;
    }
}

public class B{

    // Use var 1 from class A as following
    A a = new A();
    int x = a.getVar1(); //x has the value of Var1 now
    a.setVar1(2); // var1 in class A has value 2 now.
}

Use interfaces rather than directly call a method of another class.

public class A {
    private InterfaceB interfaceB;

    interfaceB.method1(var1, var2, var3);
}

public interface InterfaceB{
    public void method1(int var1, int var2, int var3);
    public void method2(int var2, int var3);
}

public class B implements InterfaceB{

    @Override
    public void method1(int var1, int var2, int var3){
        //doSomething
        method2(var2, var3);
    }

    @Override
    public void method2(int var2, int var3){ 
        //doSomething
    }
}
Esty
  • 1,882
  • 3
  • 17
  • 36
1

You should read about encapsulation.

Passing 3 variables encapsulated in 1 object with appriopriate accessors sounds like a better option to me (at least the code looks a bit cleaner).

Also, think of creating a utility class with static methods if it makes sense of course - sometimes you do not need class member fields at all because there is no state to this class (Math class is an example) and static methods that return the result of some calculation/transformation is a better option.

On a side note I can recommend you considering "Program to an interfaces" principle. You can read the relevant section right on the top of this page.

aviad
  • 8,229
  • 9
  • 50
  • 98
0

In B class, you declare a instance of A class, variables in A class is public. when you can use variable in A class.

Quan Nguyen
  • 698
  • 1
  • 9
  • 19
0

Here is my 2 cents...

public class Sample {
    //Property of the class
    private int valueA;

    //method to do some operation
    //that relies explicitly on a 
    //property of the class
    public void doSomething(){
        //valueA is over 9000!?
        int valueA = valueA + 9000;
    }

    //Method that does something that does not explicitly
    //rely on a property of the class
    //could be called from this or another class
    public int doSomeOperationWithSomething(int something){
        return something++;
    }
}

Another alternative would be to create a static "utility" class for your methods

public class Utils{
    public static int doMagic(int var){
        return var * var;
    }
}

used like this,

int num = Utils.doMagic(9);

These come about when you have some code that does that one useful thing, but you just can't figure out where to put it.

More importantly, you will want to maintain proper "encapsulation" (Read about that) in your code. This means limiting access to variables by other classes and allowing access to only what is needed.

public class Website {
    //No one should ever be able to
    //access this variable directly
    //So we set it a private
    private String article;

    //A reader should be able to get the aricle
    public String getArticle(){
        return article;
    }

    //The reader should never be able to set
    //an aticle on the website only read it
    //You can leave this part out or
    //set the method to private as i did.
    private void setArticle(String article){
        this.article = article;
    }
}

public class Reader {
    //Reference to website
    private Website website;

    public Reader(){

        ...

        //the user can read an article
        website.getArticle();

        // but this is not available to them
        website.setArticle("Some text"); // results in ERROR
    }
}
CarefreeCrayon
  • 249
  • 2
  • 7