4

I am aware about the constructors and what they do?

Suppose there is a class:

class Kc
{
 int a=989;
 Kc(int a)
 {
     this.a=a;
 }
 public static void main(String []args)
 {

     Kc obj = new Kc(90);
     System.out.println(obj.a);
 }
}

The output of the above program comes out to be 90. My question is this?

Why are we allowed to use the line

 int a = 989;

The output being 90 means it must have been executed before the constructor is executed so this means that we are assigning a value to an object's variable that has not yet been created? I am really confused?

J J
  • 165
  • 6
  • Your confusion isn't with variables, it's with constructors. A constructor doesn't create an object, it initializes its state. – Sotirios Delimanolis Jul 03 '15 at 17:18
  • 1
    Put a 'System.out.println(this.a)' on the line before 'this.a = a' and debug to see what happens. – M Y Jul 03 '15 at 17:22
  • @hellyale I did what u said and the result is cause of my confusion. – J J Jul 03 '15 at 17:29
  • Just refresh your page. – Sotirios Delimanolis Jul 03 '15 at 17:30
  • Did it print 989 followed by 90? – M Y Jul 03 '15 at 17:31
  • @SotiriosDelimanolis The link you provided has some extends keyword? I am beginner in java? So please post appropriate links or remove the duplicate tag. – J J Jul 03 '15 at 17:34
  • @hellyale Yes it did print that. – J J Jul 03 '15 at 17:35
  • Are you asking me if you are a beginner? Being a beginner does not change the fact that the duplicate contains an answer to your question. [EJP's answer](http://stackoverflow.com/a/14806340/438154) in particular explains the exact order of execution for a class' field initializers and constructor. – Sotirios Delimanolis Jul 03 '15 at 17:35
  • @SotiriosDelimanolis PLEASE READ MY QUESTION. I never asked for the order? – J J Jul 03 '15 at 17:39
  • _has not yet been created?_ That implies order. Field initializers are executed before constructor bodies. That's all there is to it. – Sotirios Delimanolis Jul 03 '15 at 17:40
  • @SotiriosDelimanolis Looks like you have developed some communication gap! Let me fill it with words rather than assumptions. All i want is some explanation about why non-static variables are being initialized outside constructor or function? If it was static a I would have completely understood why this was happening because they have nothing to do with objects. – J J Jul 03 '15 at 17:45
  • Because that's how the language works. You can declare instance fields and you can provide them with an initialization expression. If you do, that expression (and the corresponding assignment) is performed before the constructor. – Sotirios Delimanolis Jul 03 '15 at 17:46
  • @SotiriosDelimanolis I am still giving you a chance to post a better link. – J J Jul 03 '15 at 17:47
  • @JJ put `if (a > this.a) {this.a = a}` inside the constructor, it should print 989 twice then, maybe that will help clear up the confusion? – M Y Jul 03 '15 at 17:53
  • Please elaborate on that by posting an answer. THATS HOW THE LANGUAGE WORKS shows lack of insight. – J J Jul 03 '15 at 17:54
  • @JJ when they mark a question as duplicate it cannot be answered. – M Y Jul 03 '15 at 17:54
  • 1
    If you don't think the linked duplicate answers your question, edit your question with more details clarifying what your actual question is and what hasn't been addressed by the link. Others can vote to re-open the question. – Sotirios Delimanolis Jul 03 '15 at 18:02

0 Answers0