Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Instance variables initialization

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source : Javablackbelt.com

a)Instance variables are always initiated to defaults
b)Local variables are always initiated to defaults
c)Array Elements are always initiated to defaults
d)Object references that are not initialized explicitly will have their value set to null always


Answer given to this question is a) and c).
But a) is correct when instance variables are not explictly initialized an value.
can any one clarify this?

rami
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member variables default to null. I am not sure I quite understand what you are confused about. The question is a touch vague yes, but what is it that you want to know ?
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ram Reddy:
[QB]Source : Javablackbelt.com

a)Instance variables are always initiated to defaults
b)Local variables are always initiated to defaults
c)Array Elements are always initiated to defaults
d)Object references that are not initialized explicitly will have their value set to null always

a) is correct because instance variables are always assigned to defaults .
b) is incorrect because local variables are not assigned to defaults .
You must have initialized it before use .
c) is correct because whether array variable is instance or local, its elements are assigned to defaults.
d) is incorrect because if Object reference in instance variable then it is assigned to defaults but if it is local , you must have initialized it before use ;

class Main{
String data ; //1
int [] number = new int[4]; //2
public void test(){
String name ;
name += ""; //3
int x ;
x = x + number[2]; //4
}
}

now when you say new Main()
1. data will be assigned to defaults(null) because it is instance variable.
2. number will be assigned to int[] object because it is instance variable. Its scripting variable from 0 to 3 will also be assigned to
defaults(0 for integers).
3.name will not be assigned to defaults because it is local variable.
You must have initialized it before use, Otherwise it is compile-time
error.
4. int x is local hence it must be initialized before use, Otherwise it is compile-time error .

 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class Main{
String data = "SCJP"; //1
}



My doubt is that option (a) is not correct when we initialize instance variables like the above. So option (a) looks vague.then we should avoid it while answering it right?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ram

I understand what you mean by your example.

Compiler sets the variables (only if its not local and only and only if it is not initialized, as in your case you have already initialized to "SCJP").

Bottom line is, in your example, there exists no default initialization(by the Compiler) as you have already initialized your variable! If you would not have initialized your variable at all then your variable would have got a default initialization(by the compiler) to null(as it is a String).

______________________________________________________________

This is the very same for "default constructor"(inserted by the compiler). The compiler adds a default(no-argument) constructor in a class, only and only if you did not define any other constructors.

Example1 (shows that the compiler inserts a no-argument constructor)
//This is what you have defined
class DefaultConstructorExample{

}

//This is what you get after compilation
class DefaultConstructorExample{
//compiler has inserted this no-argument constructor
//for you as you have not introduced any constructors by your own!
DefaultConstructorExample(){

}
}

Example2 (shows that the compiler DOES NOT insert a no-argument constructor)

//This is what you have defined
class DefaultConstructorExample{
DefaultConstructorExample(String someParam){
//some code goes here
}
}

//This is what you get after compilation
class DefaultConstructorExample{
//Note that the compiler did not insert a no-argument
//constructor for you as you have already
//defined a constructor. In this case, it is you that you
//have to insert a no-argument constructor.
DefaultConstructorExample(String someParam){
//some code goes here
}
}

______________________________________________________________

Hope this helped you to clear your doubt.

Kind Regards
Kris
 
Krishnamoorthy Vuyala Muralidharan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ram

a)Instance variables are always initiated to defaults

The word 'always' makes the choice really vague! Its really tricky to interpret in such a way that the answer matches what the real answer :-(!

It would have easier to understand if the choice could have been as follows:

a) Instance variables are always initiated to defaults,if it is not already initialized!

In the above choice/option there exists no ambiguity!

Best Regards
Kris
reply
    Bookmark Topic Watch Topic
  • New Topic