Home > DeveloperSection > Forums > non-static method in java
Lady Bird Johnson
Lady Bird Johnson

Total Post:20

Points:140
Posted on    July-09-2013 2:06 AM

 Java Java 
Ratings:


 1 Reply(s)
 917  View(s)
Rate this:
Hi developers!

i have problem in following code.can you rectify it please

Thanks in advance.
This is my source code:
JCatalog:

package newpackage;
import java.awt.Component;

public class JCatalog extends javax.swing.JPanel {


    public JCatalog() {
        initComponents();
    }

    public Component getComponent() {
        return this;
    }
JPanelTicket:

package javaapplication5;
import javax.swing.JPanel;
import newpackage.JCatalog;


public abstract class JPanelTicket extends JPanel {

    JCatalog jcat;

    public JPanelTicket() {
        initComponents();
    }

    public void init(){
        jcat = (JCatalog) JCatalog.getComponent(); //Here's the problem
    }


shreesh chandra shukla
shreesh chandra shukla

Total Post:1

Points:7
Posted on    July-09-2013 2:12 AM

Hi


solution:
at the core of static methods, you know the only class name is able to call the static method of it's own.
so!You cannot call a non-static method using class, like JCatalog.getComponent()
A non-static method can only be called with instance jcat.getComponent()
I see that you have an reference maintained of type JCatalog named jcat. Make sure you initialize it somewhere OR set the instance using setter method. And use this instance to call the getComponent() method
public abstract class JPanelTicket extends JPanel {

    JCatalog jcat;

    public JPanelTicket() {
        jcat = new JCatalog();
        initComponents();
    }

    public void init(){
        jcat = jcat.getComponent(); //Here's the problem
    }
}
....................................................................................................
alternatively
.............................................................................................
public abstract class JPanelTicket extends JPanel {

    JCatalog jcat;

    public JPanelTicket() {
        initComponents();
    }

    public void init(){
        jcat = jcat.getComponent(); //Here's the problem
    }

    public void setJCatalog(JCatalog jcat) {
        this.jcat = jcat;
    }
}
i hope this code will be helpful for you.



Don't want to miss updates? Please click the below button!

Follow MindStick