Results 1 to 7 of 7
  1. #1
    ghostrider is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default instance variables initialization

    Class Dog
    {

    Dog a;
    Dog b = new Dog();
    a=b;\\error class a not found
    }
    But why cannot we initialize instance variables in two steps?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: instance variables initialization

    The assignment statement needs to be in a method or perhaps a static block.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ghostrider is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: instance variables initialization

    Can you please give me the reason behind this?

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default Re: instance variables initialization

    When you define a class, it is generally made up of 3 parts:
    -Member variables
    -Constructor(s)
    -Methods(setters/getters and other private/public methods)

    You may also have inner classes and a few other parts, but this is generally the makeup.

    As far as variable initialization goes, if you want to do it outside of a method or the constructor, I believe you have to
    declare and initialize it in one step:

    Java Code:
    Dog b = new Dog();
    If you want to declare and initialize a variable in 2 separate steps(as you have)...
    Java Code:
    Dog a; //declare
    Dog b = new Dog();
    a = b; //initialize

    ...the declaration can happen outside of the constructor or
    method like you have it, but, the initialization has to take place inside of the constructor or a method.

    I may not be explaining it correctly, or this may be confusing, but I think that is the general principle.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: instance variables initialization

    <nitpick>
    Declaring a Dog variable within the Dog class usually causes problems. Only do this if you are 100% sure you know what you are doing. Why is it a problem? It can lead to infinite recursion, ie Creating a Dog creates another Dog inside, which creates another Dog, which creates another Dog and so on.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default offtopic

    Yes, could be a problem with Dog. Not so, Flea, however, according to The Siphonaptera.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: instance variables initialization

    Quote Originally Posted by Junky View Post
    ie Creating a Dog creates another Dog inside, which creates another Dog, which creates another Dog and so on.
    Happens every year or so though. And not just one -- usually 5 or 6.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Class Instance initialization fails
    By JOHNINALBANY in forum New To Java
    Replies: 3
    Last Post: 07-08-2012, 07:43 PM
  2. Instance Variables
    By TriSBR in forum New To Java
    Replies: 2
    Last Post: 11-16-2011, 04:16 PM
  3. Instance variables
    By lala in forum New To Java
    Replies: 1
    Last Post: 01-26-2011, 04:38 PM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. static are instance variables
    By gabri in forum Advanced Java
    Replies: 12
    Last Post: 09-30-2008, 06:30 PM

Tags for this Thread

Posting Permissions

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