Question

I've been learning Java recently in my search for a programming language that isn't a nuisance to code GUI with (is it too much to ask for an easy way to make a window and add interface elements?), and I'm somewhat curious as to how dynamic and static programming intermingle in Java. Java asks me to declare my main class/entry point as:

public static void main(String[] args)...

Within main, I may declare instances with the following:

JFrame frame = new JFrame();

Why is that, as opposed to frame = new JFrame(); perfectly valid in Java?

Inn particular, when I mentally 'parse' the first, I read it as, "frame, an instance of JFrame, is a new instance of JFrame". To me, that seems somewhat redundant. The latter, which I read as, "frame is a new instance of JFrame" seems much more reasonable.

Moving on, I often, near the top of the class, define the variables that it will frequently use, especially constants or persistent display objects, so when I make a simple class, I usually declare something like public JFrame frame.

Is the redundancy in the proper instantiation, JFrame frame = new JFrame() the reason that I can delete these declarations with no apparent problem?

    public class Display {
        //The line below can be deleted with no apparent difference
        public JFrame frame; 

        public static void main(String[] args) {
        /* Why do I need to declare the type of a variable I have 
         * already declared above? */
        JFrame frame = new JFrame();

        frame.setSize(800, 600);
        frame.setVisible(true);
        }
     }

In the case that I delete that declaration up top, am I right in assuming that frame becomes a child of the main method? In that case, declaring it up top makes it a child to Display?

Overall, I would like an explanation of instantiation through a static class. I really hope that I'm not asking a silly question, but I have found that online 'tutorials' usually take the form of a walkthrough that describes what the code does, but not why it is used that way; basically, I can't find the answer elsewhere and I know you guys have a lot of knowledge. I would really appreciate a thoughtful response, and if possible, the explanation of how to interact with the declared 'frame' in each of my examples.

I would also be quite happy for an explanation of why frame = new JFrame is invalid, even if I declare public JFrame frame up top.

Thank you very much for reading this and taking the time to respond ^_^

Was it helpful?

Solution

There's a very significant difference between:

JFrame frame = new JFrame();

and

frame = new JFrame();

The first version declares a new local variable (in the context you've given) and assigns it a value. That local variable is completely separate to the instance variable you declared within the Display class.

The latter snippet assigns a new value to an existing variable - except in this case it won't work because you have an instance variable but no instance of Display.

If you declare your frame variable as static rather than as an instance variable, you can assign to that within your main method:

public class Display {
    public static JFrame frame; 

    public static void main(String[] args) {
        frame = new JFrame();
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
 }

Or you could create a new instance of Display and assign a value to an instance variable within it:

public class Display {
    public JFrame frame; 

    public static void main(String[] args) {
        Display display = new Display();
        display.frame = new JFrame();
        display.frame.setSize(800, 600);
        display.frame.setVisible(true);
    }
 }

However:

  • There's no indication that you're going to use the instance/static variable within Display anyway
  • I'd strongly encourage you to always make your fields private

OTHER TIPS

//The line below can be deleted with no apparent difference
public JFrame frame; 

Yes, because it is an unused variable.

You are never using the instance field frame in your code.

Instead you have

 public static void main(String[] args) {
    /* Why do I need to declare the type of a variable I have 
     * already declared above? */
    JFrame frame = new JFrame();

which declares a new, completely unrelated local variable of the same name and type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top