Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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

constructor

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class QuestionOfTheDay3{
int var1 = 1;
final int var2 = 2;
static int var3 = 3;
QuestionOfTheDay3(){
this(??); // ??
}
QuestionOfTheDay3(int k){
System.out.println(k);
}
public static void main(String args[]){
QuestionOfTheDay3 q3 = new QuestionOfTheDay3();
}
}
Which of the following variables can be replaced ?? without compiler error?
A1 )var1
A2 )var2
A3 )var3
A4 )All the above.

Answer is var3

Please tell me why constructor can call the instance variable and final variables.

is it right that constructors shouldnever call the instance variables and should always call the static variables???
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anand:
Is it right that constructors shouldnever call the instance variables and should always call the static variables???

assume class B is subclass of class A and if there is An explicit call to the super clss constructor with arguments using super(arguments)
the arguments shld be static.i.e.either in the current class or any subclass

as only static vars u can refer to ,u cant have a reference to instance var while control is in the constructor

plz do correct me if i am wrong
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Constructors are invoked to intiallize instance variable i.e the state of an object. While static variable are not associated with an object. They get created while class loading. so when constructor is called to create object static variables are available for access.

So we can't refer to instance variable when explicitly calling a constructor

This is as per my knowledge
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yuo cannot refer to the instance variables when you are calling the constructor explicitly in the constructor..

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if there are no static variable then what does the constructor do ??
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue is actually a little bit subtle.

You actually can use instance variables in a constructor, you just have to know when they are actually initialized. Initialization of instance variables and execution of instance initializers are done before the constructor, but after the constructor of the super class.

In this case, the implied super() call is not done til just before the questionoftheday(int) constructor. This means that the instance variables has not been initialized at the time they are used in the the this() call of the default constructor.

Henry
[ September 21, 2005: Message edited by: Henry Wong ]
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic