Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about contructor

 
Deedy Dee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are member variable ?
what are instance variable?
Is "contructor initialize all the variable" correct?
 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "contructor initialize all the variable" correct?
the answer should be NO
Proved by code:
------------------------------
class TestConstructor
{
TestConstructor()
{
int a;
int b;
a=b;

}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

-----------------------------
---------- javac ----------
TestConstructor.java:8: variable b might not have been initialized
a=b;
^
1 error
Normal Termination
Output completed (5 sec consumed).
 
Varaku Venkata
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by zhihui di:
what are member variable ?
what are instance variable?
Is "contructor initialize all the variable" correct?

member variable is one if it is a part of the class memeber like methods. constructors will not initialize these variables. and these variables should be initialized before use.
instance variable is one if it is a part of object. it is not declared as static. and all objects contain instance variables.
constructors initializes the instance variables if they are not initialized. (except in some cases like given above)
[ March 20, 2002: Message edited by: varaku ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
varaku,
Welcome to Javaranch
Please read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Member variables are variables that are declared at the class level and not within methods or any other block declarations (initializers, etc).
Member variables are further separated into two groups: class variables and instance variables.
Class variables (or static fields) are those variables declared static. They are initialized when the class itself is initialized and it is the same variable for all instances of the class.
Instance variables are those variable not declared static. They are initialized everytime a new instance of the class is created just before the constructor is invoked.
Class and instances variables are assigned default values (4.5.5 Initial Values of Variables) when the class, respectively, the instance are initialized.
[ March 21, 2002: Message edited by: Valentin Crettaz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic