Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access private variable from other class in java

Tags:

java

oop

i hope that i mean my words. i have a class like this:

public class MainClass extends JFrame{
    private JLabel mainlabel;
    private SampleClass sample=new SampleCalss();

    public void intital(){
        mainlabel=new JLabel("Main");
        sample.setMethod(getLabel());
        //
        //some code
        //
        add(mainlabel); 
    }

    public static void main(){
        intital();
    }

    public JLabel getLabel(){
        return mainlabel;
    }
}

and other class like this:

public class SampleClass extends JFrame{
    private JButton button=new JButton("Change");
    private JLabel sLabel;

    public SampleClass(){
        //somecode
        //
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sLabel.setText("Sample text set");
            }
        });
        add(jButton);
    }

    public void setMethod(JLabbel l){
        sLabel=l;
    }
}

is this a true way to access mainlabel and change its value from other class(in this sample code in class SampleClass) is there a better or right solution? note that MainClass is class that has main method.

like image 837
mehdi Avatar asked Nov 01 '11 06:11

mehdi


People also ask

Can we access private variable from another class in Java?

We can call the private method of a class from another class in Java (which are defined using the private access modifier in Java). We can do this by changing the runtime behavior of the class by using some predefined methods of Java. For accessing private method of different class we will use Reflection API.

Can you access as private variable outside of it's class?

Private and Public Access You can read or write to the variable from anywhere within the module, class, or structure, but not from outside it.


1 Answers

The correct way to access a private variable from another class is with getter and setter methods. Otherwise, you should have made that variable public.

That is:

// getter
public JLabel getMainLabel() { 
    return mainlabel;
}

// setter
public void setMainLabel(JLabel mainLabel) {
    this.mainlabel = mainLabel;
}

However, it is a bad practice to return private data directly - that allows external code to modify your private state. In general, you should return a copy of your private data so that external code can't mess with the internals of your class. But if you need external code to call methods on your private data, then you should probably be providing manipulation methods in your class, rather than directly exposing private data.

You probably really want to create methods like setText() and getText() in your main class, and then call the setText() and getText() methods on mainlabel. However, you need to be careful with this, as you might be inclined to replicate every single method defined by JLabel in your class. That would tightly couple both your class and its consumers wit the JLabel implementation. If you choose to replace the JLabel with something else in the future, it will take a lot of work to unwind the coupling you've created.

like image 115
ObscureRobot Avatar answered Sep 28 '22 07:09

ObscureRobot