0
public class MainMDI extends javax.swing.JFrame {

   private static MainMDI thiz; 

      public MainMDI() {
        initComponents();
        thiz = this;
      }
}  

I'm creating an MDI application in swing. Class MainMDI is the main class of the application and therefore the main method resides in that class. The above code creates a static variable called thiz that points to the instance of class MainMDI when the application runs.

I'm planning to use variable thiz to access non-static (instance) members of class MainMDI from within the main method.(I cannot access non-static members from from within the main method since the main method is a static member in class MainMDI in my application).

public class MainMDI extends javax.swing.JFrame {

   private static MainMDI thiz = this; 

      public MainMDI() {
        initComponents();
      }
}  

But when I attempt to initialize variable thiz as in the above code, compiler says non-static variable this cannot be referenced from a static context. But I'm not referring to this in a static context here am I? Is this because variable this, being non-static, is not yet initialized when the static variable this is initialized?

Also, would it have been a better programming practice if I had not set class MainMDI as the main class and created another class with a main method in it and set that class as the main class?

13
  • 1
    Looks like you want a singleton. Google for that Commented Mar 12, 2013 at 7:26
  • 1
    @Prashan By providing a (aka one) static variable which references an (aka one) instance of your class, you effectiviely limit the number of accessible instances of your class to one (at least in the context in which you think you have to get access via a static method). Having several instances and making only the last one available via the static thiz is something one would solve differently, so everyone is implicitly excluding this option. Hence, we all think you really want a singleton approach. If you keep insisting on answering no!, then you'll have to describe your scenario. Commented Mar 12, 2013 at 8:12
  • 1
    @Prashan Why don't you just keep a reference to the object? I have no clear picture of your software architecture yet but I presume this is not the only reference you'll need in your application. In such cases, I would register the object with an "environment" object which is available wherever I need it. It could even be static because it's certain that I'll have only one environment in my application. Still, should I find that I need more MainMDI object references, code changes would be limited to the environment class and the few places I need MainMDI. Commented Mar 12, 2013 at 8:39
  • 1
    @Prashan And JInternalFrame.getParent() does not rid you of keeping the reference to the parent JFrame separately? Commented Mar 12, 2013 at 9:16
  • 1
    @Prashan I wasn't referring to main, really, but to the places in the code when you need access to your MainMDI object. There must be a reason why you want to access it so you'll have a bit of context available, no? I was thinking the context might be a JInternalFrame object. -- Either way, the decision is yours, we all have pointed you towards some ways to do this as it should be done, so enjoy, and I hope you succeed. Commented Mar 12, 2013 at 9:40

2 Answers 2

5

But when I attempt to initialize variable thiz as in the above code, compiler says non-static variable this cannot be referenced from a static context. But I'm not referring to this in a static context here am I?

Yes, you are. Static class variables are initialized when the class is loaded (not when an object instance is being created). There is no this in that context. The code:

private static javax.swing.JFrame thiz = this; 

Will simply not work. Despite your assertions to the contrary, you do want a singleton. Otherwise, given N possible object instances of your MainMDI object, which one would you be expecting to access from a static context? You should consider refactoring your code rather than trying to strong-arm around Java language semantics.

Sign up to request clarification or add additional context in comments.

4 Comments

Still nope. I've no need for a singleton class because there's no danger of class MainMDI being instantiated multiple times in my application. Point taken about refactoring code, but I can't help "strong-arming". That way I stumble upon problems like this and learn something out of it.
I think you do need a singleton Prashan. Not to prevent multiple instantiations of your object, but to support the initialization of a field from a static context like you are trying to do. But anyway, if the object is only instatiated once, why on earth do you need thiz?
@vikingsteve Thanks for the comment. How exactly could singleton support initialization of a field from a static context? I'd like to know more. And to answer your question, if you read the question properly you will understand why on earth. I NEED variable thiz to access and only to access instance members of class MainMDI from within the main method. (Main method is static, and its inside class MainMDI. This effectively makes all the instance members of class MainMDI inaccessible to the main method directly OR via the variable this. So I'm using thiz to access them)
Well, I think you can either instantiate MainMDI in your main method (avoiding the need for accessing members from a static context completely, if you can), or try a Singleton with Eager initialization like this: private static final javax.swing.JFrame INSTANCE = new MainMDI();
3

this means "the object instance currently being operated on", it only makes sense inside a non-static member function. In general this is implicitly passes to each non-static member function when you call that member function so it'd be fair to say that it is initialized right before a non-static member function gets called.

Whether factoring out a class with "main" method is a good idea will heavily depend on actual implementation details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.