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?

PrashanD
  • 2,643
  • 4
  • 28
  • 58
  • 1
    Looks like you want a singleton. Google for that – Bohemian Mar 12 '13 at 07:26
  • Nope, I've no need to make class MainMDI singleton. I just want to have the main method inside class MainMDI, and I want the main method to have access to instance members of class MainMDI. – PrashanD Mar 12 '13 at 07:28
  • 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. – class stacker Mar 12 '13 at 08:12
  • @ClassStacker Thanks for the comment. Here's the scenario: Note that class MainMDI extends JFrame. In the main method it's instantiated as "new MainMDI().setVisible(true);". This JFrame, once instantiated acts as the parent of several JInternalFrames in my app. I believe this instantiation is only going to happen once. The main method will only run once from top to bottom. Only a single instance of class MainMDI will be created. I won't have several instances and only the last one available via "thiz". I will have only one instance and that, will be available (for static method main) via thiz. – PrashanD Mar 12 '13 at 08:27
  • 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. – class stacker Mar 12 '13 at 08:39
  • Well, the new MainMDI().setVisible(true); statement is inside an anonymous innerclass "new Runnable(){}". So I picked this alternative over creating and keeping a reference to the instance of class MainMDI inside the Runnable inner class and then accessing it. – PrashanD Mar 12 '13 at 09:00
  • possible duplicate of [Nested class: Cannot access non-static field in static context](http://stackoverflow.com/questions/10988878/nested-class-cannot-access-non-static-field-in-static-context) – user207421 Mar 12 '13 at 09:06
  • 1
    @Prashan And [JInternalFrame.getParent()](http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html#getParent()) does not rid you of keeping the reference to the parent `JFrame` separately? – class stacker Mar 12 '13 at 09:16
  • JInternalFrame's reference variable is non-static and its declaration code is generated and edit-locked by the Netbeans IDE. Can't use it inside static method main. I could access it via variable thiz though. thiz.jInternalFrame1.getParent(); – PrashanD Mar 12 '13 at 09:26
  • 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. – class stacker Mar 12 '13 at 09:40
  • Thanks. I hope so too. A lot depends on it. – PrashanD Mar 12 '13 at 09:52
  • @Prashan So you promised to do something that you can't? Want to know my hourly rate? ;) – class stacker Mar 12 '13 at 09:55
  • @ClassStacker If I do this myself it will increase my hourly rate. But tell me anyway. – PrashanD Mar 12 '13 at 14:39

2 Answers2

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.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • 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. – PrashanD Mar 12 '13 at 07:47
  • 2
    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 Mar 12 '13 at 07:52
  • @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) – PrashanD Mar 12 '13 at 07:58
  • 1
    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](http://en.wikipedia.org/wiki/Singleton_pattern#Eager_initialization) like this: `private static final javax.swing.JFrame INSTANCE = new MainMDI();` – vikingsteve Mar 12 '13 at 08:31
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.

sharptooth
  • 167,383
  • 100
  • 513
  • 979