JarvisLYu1
JarvisLYu1

Reputation: 9

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

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' "

Upvotes: -1

Views: 155

Answers (1)

Bro_dell
Bro_dell

Reputation: 71

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

Upvotes: 1

Related Questions