हिंदी

Can member variables of a Java class be initialized when the class is defined?

Advertisements

प्रश्न

Can member variables of a Java class be initialized when the class is defined?

दीर्घउत्तर
Advertisements

उत्तर

  • Yes, member variables of a Java class can be initialized at the time of their declaration within the class definition. These variables are declared inside a class but outside any method, constructor, or block.
  • Both instance variables and static (class) variables can be assigned initial values directly when they are declared.
  • If member variables are not explicitly initialized, Java automatically assigns default values according to their data types. For example, numeric variables receive 0 or 0.0, boolean variables receive false, and reference variables receive null.

Thus, Java allows member variables to be initialized during class definition, but explicit initialization is not compulsory.

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 6: Variables and Expressions - SHORT ASSIGNMENTS [पृष्ठ १३६]

APPEARS IN

धीरज मेहरोत्रा Computer Science [English] Class 12
अध्याय 6 Variables and Expressions
SHORT ASSIGNMENTS | Q 39. | पृष्ठ १३६
Advertisements
Share
Notifications



      Forgot password?
Use app×