0

I've just learned about init block. I know both(constructor & non-static init block) can be used to initialise members of a class . So, I wanted to know which out of the two is better/more_useful for initialisation of members of a class and what is the reason for it. I have read Use of Initializers vs Constructors in Java but I don't think it answer my question(if had miss anything in this question then please mention it and explain it in easy words.)

Note: I am asking for initialisation of non-static members only.

Thankyou in Advance

Community
  • 1
  • 1
  • 3
    Also this one: http://stackoverflow.com/questions/1355810/how-is-an-instance-initializer-different-from-a-constructor – Tunaki Jun 10 '16 at 07:20
  • @Andy please help me to point out the line which answer my question bcz I am unable to find it –  Jun 10 '16 at 07:24
  • 1
    Can I point out "the line"? No - it is not a simple question, and the answers (in both the duplicate I have marked and that suggested by Tunaki) are correspondingly long. The best way I can summarize it is "use a constructor by default; only think to use an instance initializer if you find that you really need it". – Andy Turner Jun 10 '16 at 07:31
  • @Tunaki thanks for directing me to correct answer . Thankyou :) –  Jun 10 '16 at 07:31

1 Answers1

0

If you want to initialize a particular thing lots of times then go for non static else go for constructor. For example

public class test{
    int i;//you want to initialize i to 100 when any object is created then go for non static
    {
        i=10;
    }
    test(){}
    test(int x)
    {
    }
    test(String y)
    {
    }

    public static void main(String args[])
    {
        test t=new test();// i will be 10 during this
        // 
        test t1=new test("hello");// if you call this constructor also i will be 10
        //
        test t2=new test(10);// even if you call this constructor i will be 10
    }

If you wanted to initialize in constructors then there will be code duplication

public class test{
    int i;//there will be code duplication if you initialize in constructors


    test(){i=10;}
    test(int x)
    {
        i=10;
    }
    test(String y)
    {
        i=10;
    }

    public static void main(String args[])
    {
        test t=new test();// i will be 10 during this
        // 
        test t1=new test("hello");// if you call this constructor also i will be 10
        //
        test t2=new test(10);// even if you call this constructor i will be 10
    }

Again it will be a problem if you want to initialize i to 100 then you have to change in all constructors but if you go for non static way then you have to change in only one place

Draken
  • 3,134
  • 13
  • 34
  • 54
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • by "lot of things " do you mean every single object that I create of my class and do we have any other reason to go for init block ? –  Jun 10 '16 at 07:21
  • @PrayagSharma Please read the updated answer – SpringLearner Jun 10 '16 at 07:24
  • I don't quite agree to "there will be code-duplication": a constructor can call other constructors. – TmTron Jun 10 '16 at 07:27
  • @TmTron As per the example above, isnt i=10 duplicated ? – SpringLearner Jun 10 '16 at 07:28
  • in your code example it is. But it would be much better to call `test();` instead: e.g. in the `test(int x)` constructor – TmTron Jun 10 '16 at 07:31
  • 1
    @TmTron What if different constructor does different functions e.g `test(){i=10;sysout("no arg constructor is only called")} test(int x){i=10;sysout("int arg constructor is only called");` – SpringLearner Jun 10 '16 at 07:33
  • yes. then it would make sense - you should update your answer to reflect this (also you mix up 10 and 100 in your example). Anyway, even it his case, there are [alternatives](http://stackoverflow.com/a/3386148/6287240), which should be mentioned. – TmTron Jun 10 '16 at 07:46