How can I create a new class and create a new variable of its type?

147 views Asked by At
import javax.swing.JFrame;
public class XFrame extends JFrame {

}

I saved with name XFrame and compiled it, no bug. But when I want to create a new variable of type XFrame in the interaction pane:

new XFrame()

It showed "Static Error : Undefined class 'XFrame' "

1

There are 1 answers

2
Bro_dell On

If you're trying to create an object of type XFrame you need to declare a variable that refers to XFrame. For example you could try

XFrame myFrame = new XFrame();

EDIT

You need to create the object in the main method

import javax.swing.JFrame;
public class XFrame extends JFrame {

}

public static void main(String [ ] args)
{
      XFrame myFrame = new XFrame();
}

Here's a link for more info about the main method
https://www.journaldev.com/12552/public-static-void-main-string-args-java-main-method