Results 1 to 4 of 4
  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Where to initialize data? (Object and classes)

    which is more preferable way of initializing data? outside or inside constructor and why?

    Java Code:
    private double height = 1;
    private double width = 1;
    
    Rectangle(){
    }
    or

    Java Code:
    private double height;
    private double width;
    
    Rectangle(){
       height = 1;
       width = 1;
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Where to initialize data? (Object and classes)

    You can also do both, and, technically, when you are defining them in the constructor you ARE doing both as, until the constructor is called, they will get a "default" value depending on type. It doesn't, 99.9999999999% of the time, matter. The variables are intialized before the constructor is ever called and there is, possibly, a very minimal window where they might be used before the constructor is ever called, but you would have to do something extreme, AFAIK, for this to play a role. IOW, normally, a programmers preference choice.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Where to initialize data? (Object and classes)

    Masijade is correct and the issue is primarily one of coding style. Sometimes initializing during declaration makes sense - like with constants or with some default initial values. It can get messy if you try to do too much during initialization though - and this can happen with objects and anonymous classes.

    Generally you want to keep things clean and understandable in your code. If moving a lot of init code into the constructor or into it's own method makes sense for clarity, then do so. Clarity and cleanliness are generally most important, and I often introduce helper methods/classes if they make the logic easier to follow.

    tl;dr: if you're just initing a couple simple values like ints or booleans, doing it during initialization is totally fine. If you're building objects, creating anonymous classes, chaining methods, etc... do it in the constructor to keep your declarations tidy.

  4. #4
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Where to initialize data? (Object and classes)

    I see. Thanks for the clarifying

Similar Threads

  1. Replies: 11
    Last Post: 04-11-2012, 06:57 PM
  2. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 02:39 AM
  3. Replies: 3
    Last Post: 09-29-2010, 10:26 AM
  4. How to show class initialize data in JSP ?
    By tking88 in forum New To Java
    Replies: 0
    Last Post: 03-07-2010, 10:17 AM
  5. Replies: 1
    Last Post: 04-17-2008, 11:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •