Open In App
Related Articles

Java | Constructors | Question 2

Improve Article
Improve
Save Article
Save
Like Article
Like

Predict the output of following Java program




class T {
  int t = 20;
  T() {
    t = 40;
  }
}
class Main {
   public static void main(String args[]) {
      T t1 = new T();
      System.out.println(t1.t);
   }
}


(A) 20
(B) 40
(C) Compiler Error


Answer: (B)

Explanation: The values assigned inside constructor overwrite the values initialized with declaration.

Quiz of this Question

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Similar Reads