1

I am working on a class with an inner class

My code:

package com.solignis;
public class Config {
    public static final Target target;
    class Target {
        public void create(String targetName) {    
            System.out.println("Created" + targetName);   
        }

        public void destroy(String targetName) {    
            System.out.println("Destroyed" + targetName);    
        }
    }    
}

IntelliJ doesn't see anything wrong with the subclass but it keeps complaining that I have not initalized the static variable target. But when I try to initialize it with something like null I get a null pointer exception (no surprise there!) but I don't what I could initialize the variable with since as far I can understand all its just an instance of the Target subclass in the Example superclass (is this right?). Also Target has no constructor so I cannot declare new on target in order to initialize the variable.

So what could I do?

Please correct me if I am incorrect about my understanding of this I am still trying to wrap my head around the more "deeper" workings of Java.

AtomicPorkchop
  • 2,625
  • 5
  • 36
  • 55
  • If you don't define a constructor for a class, there will be a default no-args constructor. – Eng.Fouad May 25 '13 at 07:34
  • It is a surprise that you can't initialize it with null. There is a problem in you program logic. This is not a java/intellij problem –  May 25 '13 at 07:37
  • I know its a problem with my logic but I am not sure what. I was more stating that intellij is complaining about it. – AtomicPorkchop May 25 '13 at 07:38
  • Oh oops, that what I get for not copying and pasting my actual code in. The example I wrote was done specifically for this post. One sec. – AtomicPorkchop May 25 '13 at 07:40
  • @Solignis Now your question and code does not make any sense. – Smit May 25 '13 at 07:45
  • You probably want the target class to be static, so you can create an instance without an having an instance of Config. Then it would be possible to build a target using, the `create()` method. You can create a Target to use as input to the Config constructor. I do not know what you plan, but in most cases I would also advice against making the Target variable static. The reason is that it makes no sense to have a Config class if the target applies for all Configs (I guess and also since Config is not static). I might have misunderstood things, but you should look one more time at your design – patrik Jul 05 '17 at 21:46

2 Answers2

2

The variable test is indeed not initialized: public static Test test;

Initialization means putting value into the variable, i.e. something like this: test = SOMETHING;

The SOMETHING may be either null or an instance of the type of the variable. In your case:

test = null;
test = new Test();

are valid expressions.

Indeed if you put null to the test you cannot "use" it, i.e. invoke its method or access its member variables.

BTW. Test is not a subclass of Example as probably you think. It is its inner class. Subclass is a class that extends other class. Actually Test is a direct subclass of object.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
AlexR
  • 114,158
  • 16
  • 130
  • 208
1

in fact you can initialize a class that you do not declared a constructor for it it has a default constructor with no argument

    test = new Test();
Branky
  • 373
  • 8
  • 23