non-static method in java

Total Post:19

Points:133
Java 
 1459  View(s)
Ratings:
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
    }
  1. Post:1

    Points:7
    Re: non-static method in java

    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.


      Modified On Mar-30-2018 04:50:39 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!