hi! I have a problem regarding this matter. To illustrate:
i have class A, which holds a data to be passed to class B to be used in
method1 of class B. I also have class C, in which a have the event to trigger method1 in
class B. In order to execute that method1 i have to declare an object of class B.

ClassB classb = new ClassB(); //creates new instance of class B
classb.method1(); //so the data that was from class A to B is already
                  //null because i've created a new instance of class B in class C
                  //to access method1.

So, how am i going to retain the data from class A passed to class B when the method is invoked in class C?
Anyone please :)

pass the information as parameters, either to the constructor, or to a method.

You haven't provided enough info for anyone to say what is the most appropriate solution - eg when and any are instances of A,B and C created and allowed to die?
Maybe if you described what the problem really is that will help.
Here's a discussion of one such real-life situation in which a GUI display class gets info from a model class that's populated froma GUI input class - maybe that's directly relevant to you, or maybe it will give you some useful background.

Hmm, okay. It's like this

public ClassA{  //this is a form
 //some process
 //let's say we have data retrieved in here

 private void okButtonevent(e){
    //so here im going to pass the data
    //to classB


 }
}

public ClassB{
 protected String data;
 public setData(String data){
    this.data = data;
 }

 public String getData(){
    return data;
 } 

 public method1(String something){
    //method to manipulate the data from classA
 }
}

public ClassC{
  //this is another form
  //a have a button to invoke the method1 of classb
  //so that the data from classA will be manipulated

 public void buttonevt(e){
    //so in here, if i do this:
    ClassB classb = new ClassB(); 
    //creates a new instance. therefore, if i:
    String d = classb.getData();
    //d i null. the data from ClassA no longer exist
    //because i was creating a new instance. 
 }
}

So, my problem is how to invoke method1 from classC so that data will not be null.
btw, ClassC extends to an abstract class.

Its because you're making a new instance of class B that is compeltely different to the one holding the data. That is why you're getting a null return.

I would solve this by making a "control" class that works as a communication class between all of the classes you have. So make a new class that creates instances of A, B and C.

C calls a get method in the control class which calls the get method in B. B returns the data to the control class which returns the data to C.

public ControlClass() {

    ClassA classA = new ClassA();
    ClassB classB = new ClassB();
    ClassC classC = new ClassC();

    public String getTheData() {

        return classB.getData();

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.