Results 1 to 7 of 7
- 08-23-2012, 08:53 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
- 08-23-2012, 09:13 PM #2
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.
- 08-24-2012, 05:30 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
Re: instance variables initialization
Can you please give me the reason behind this?
- 08-24-2012, 08:34 PM #4
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:
If you want to declare and initialize a variable in 2 separate steps(as you have)...Java Code:Dog b = new Dog();
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.
- 08-27-2012, 03:48 AM #5
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.
- 08-27-2012, 04:02 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
offtopic
Yes, could be a problem with Dog. Not so, Flea, however, according to The Siphonaptera.
- 08-27-2012, 07:00 AM #7
Similar Threads
-
Class Instance initialization fails
By JOHNINALBANY in forum New To JavaReplies: 3Last Post: 07-08-2012, 07:43 PM -
Instance Variables
By TriSBR in forum New To JavaReplies: 2Last Post: 11-16-2011, 04:16 PM -
Instance variables
By lala in forum New To JavaReplies: 1Last Post: 01-26-2011, 04:38 PM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
static are instance variables
By gabri in forum Advanced JavaReplies: 12Last Post: 09-30-2008, 06:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks