dgupta3091
dgupta3091

Reputation: 1197

Default Constructor not called while creating object in Java

public class StaticFinalExample {
    static String str;

    public void StaticFinalExample() {
        System.out.println("In Constr");
        str = "H";
    }

    public static void main(String[] args) {
        StaticFinalExample t = new StaticFinalExample();
        System.out.println(str);
    }
}

In above example the output is null.

Why was not the constructor called?

Upvotes: 0

Views: 105

Answers (2)

Pushpendra
Pushpendra

Reputation: 55

Avoid using class name as a method name, it's ambiguous. When we notice any name having same value as class, our mind reads as a class name not as actual usage (method name in your case).

It's not a good practice. It does not mean you can not use method name as class name, but you should avoid using same name.

Upvotes: 0

Sankeeth Ganeswaran
Sankeeth Ganeswaran

Reputation: 477

Constructors don't have a return type. There shouldn't be void in your StaticFinalExample() method, if that's your constructor.

Upvotes: 3

Related Questions