This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes automatic variable has not been initialized. problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "automatic variable has not been initialized. problem." Watch "automatic variable has not been initialized. problem." New topic
Author

automatic variable has not been initialized. problem.

Pankaja Shinde
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
1. class Car
2.{
3.static int milesPerGallon;
4.int index;
5.
6.Car(int mpg) {
7.milesPerGallon = mpg;
8.index = 0;
9.}
10.Car() {
11.}
12.
13.public static void main(String[] args) {
14.int index;
15.Car c = new Car(25);
16.
17.if (args.length > 0)
18.if (args[index].equals("Hiway"))
19.milesPerGallon*= 2;
20.System.out.println("mpg: " + milesPerGallon);
21.}
22.}

Options

1. The code compiles and displays "mpg: 50" if the command-line argument is "Hiway". If the command-line argument is not "Hiway", the code displays "mpg: 25".
2. The code compiles and displays "mpg: 50" if the command-line argument is "Hiway". If the command-line argument is not "Hiway", the code throws an ArrayIndexOutOfBoundsException.
3. The code does not compile because the automatic variable named index has not been initialized.
4. The code does not compile because milesPerGallon has not been initialized.
5. The code does not compile because the no-args constructor is not written correctly


Correct Answer : 3

why ?
As i think index is initialized in the constructor call at line 8. Then why option c is correct.
Explain.

pankaj shinde

Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
The variable index declared in Class car is not static. So main method cannot access non static variables directly without having an instance of the class.
Secondly, index variable declared in main method is not initialized but trying to use it and hence the compilation error.
Antonio Trapero
Greenhorn

Joined: Mar 24, 2007
Posts: 11
Look variable index inside main. It iss shadowing instance variable inside main block. Cause it has not been initialized, you get a compilation error.
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
I guess its not even shadowing because index variable in class cannot be accessed by main() method at all.
Antonio Trapero
Greenhorn

Joined: Mar 24, 2007
Posts: 11
Yep, you are right.No shadows here.
 
Don't get me started about those stupid light bulbs.
 
subject: automatic variable has not been initialized. problem.
 
Similar Threads
Sun Cirtification
Calling constructor
collections framework doubt
Need help with Car Inventory Program part 3
assignment of final member variables