• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

about contructor

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what are member variable ?
what are instance variable?
Is "contructor initialize all the variable" correct?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic