IDKWhatImDoing
IDKWhatImDoing

Reputation: 147

class hierarchy methods when invoking from other classes

First off this is a pretty basic problem, but i just cant seem to get it, ok so here is a general overview of my program

       |class A |
       |new B();|
       |new C();|
      /         \
     /           \
|Class B |     |Class C     |
|new D();|     |method E(){}|
    |
|Class D         |
|Invokes method E|

This is my programs hierarchy and I want to invoke the non static method E from class D, without creating a new instance of class C, is there a way to do this or do i have to restructure my class hierarchy.

**EDIT:**Here is real code to display my problem(without a class B though):

import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class TheFrame extends JFrame{
public TheFrame(){
    setTitle("Suduku Solver");
    add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
            new TextLabel(),  new ChangeButton()));
    setVisible(true);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(300,300);
    setLocationRelativeTo(null);
}
public static void main(String[] args) {
    new TheFrame();
}
}


import javax.swing.JLabel;
public class TextLabel extends JLabel{
public TextLabel(){
    setText("This is the Label");
}
public void ChangeLabel(){
    setText("Changed Label");
}
}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class ChangeButton extends JButton {
public ChangeButton(){
    setText("Click to change Label");
    addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            //Invoke TextLabel changeText method here
        }
    });
}
}

i would like to invoke the changeText method by clicking the button

Upvotes: 1

Views: 89

Answers (2)

alayor
alayor

Reputation: 5035

You could inject C class to D using constructor or setters. This is an example with constructor injection.

class A {
 A() {
   C c = new C();
   B b = new B(c);
 }
}

class B {
 B(C c) {
   D d = new D(C c);
 }
}

class D {
 private C c;

 D(C c) {
   this.c = c;
 }

 public void methodThatCallsE() {
  c.E();
 }
}

This way you can call method E without having to create an object of C in D.

You could modify your ChangeButton class to receive any JLabel object.

public class ChangeButton extends JButton {
       private JLabel jlabel;
       public ChangeButton(JLabel jlabel){
          this.jlabel = jlabel;
          setText("Click to change Label");
          addActionListener(new ActionListener(){
              @Override
              public void actionPerformed(ActionEvent e) {
                  jlabel.setText("new Text");
              }
          });
      }
  }

And then, TheFrame class would look like this.

public class TheFrame extends JFrame{
public TheFrame(){
    setTitle("Suduku Solver");
    JLabel jlabel = new TextLabel();
    add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
            jlabel,  new ChangeButton(jlabel)));
...

Upvotes: 1

Bohemian
Bohemian

Reputation: 425188

You can't invoke a non-static method without an instance. That's "non negotiable".

You'll have to rework your class structure.

Upvotes: 1

Related Questions