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 Beginning Java and the fly likes local variable not assigned any value in a method yet how does it compile?? 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 » Java » Beginning Java
Bookmark "local variable not assigned any value in a method yet how does it compile??" Watch "local variable not assigned any value in a method yet how does it compile??" New topic
Author

local variable not assigned any value in a method yet how does it compile??

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055



http://www.lifesbizzare.blogspot.com || OCJP:81%
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Which variable are you talking about? z? It is actually assigned a value - the for loop assigns it to each entry of the x array in turn.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Matthew Brown wrote:Which variable are you talking about? z? It is actually assigned a value - the for loop assigns it to each entry of the x array in turn.


No i mean X , X has not been assigned any value and its local variable and as per the definations local variables cannot have default value?

so how can any value be assigned to z , what will be the values of x and z then?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Vishal Hegde wrote:No i mean X , X has not been assigned any value and its local variable and as per the definations local variables cannot have default value?

x is a method argument. The value will be supplied by whatever calls the foo method.
Atul Darne
Ranch Hand

Joined: Jul 05, 2009
Posts: 118

the value is needed here which should be passed by the calling function


Regards, Atul.
I came to this world on a Learner's License
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Note that x is a varargs parameter. Inside the method it will look like an array (an int[], to be precise). When you call the method without any arguments, x will be an array of length 0. The method will then do nothing, because there's nothing to loop over.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

But Demo class when compiled is compiled successfully...

x is a local variable and also z is a local variable for that for loop and they havent been assigned any values and they compile without giving compile error as of "x and z is not assigned any value"

It should give some error in the for loop ,right as x is not initialised with any value and nor is z and while typing

System.out.println(z);

Why is it compiling successfully and also no output is generated neither compile error is there?

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

x is not a local variable, it's a method argument.

Do you think this would give you an error ?


[My Blog]
All roads lead to JavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Vishal Hegde wrote: . . . z is a local variable for that for loop and they havent been assigned any values . . .
But z is assigned a value. That is done by the for-each loop.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Christophe Verré wrote:x is not a local variable, it's a method argument.

Do you think this would give you an error ?


Instance variable have default value 0 hence it wont produce error...But in my case both variables are in method so their scope will also be in method ...

so they are local for that method only so default assignment is not possible as they are local variables right?
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

When you call your method, you can't only call foo() - you need to supply a value for x. This value will be used by the method for x. z is assigned a value by the for-each-loop construct.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Jan Hoppmann wrote:When you call your method, you can't only call foo() - you need to supply a value for x.

In this case, you can call just foo() because the method has a varargs parameter. But inside the method, x would look like an array of length 0 when you call it like that.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Vishal Hegde wrote:



I am simply compiling this class and its compiles (not calling foo method from anywhere)

javac Demo.java

Why is it Compiling???
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Vishal Hegde wrote:I am simply compiling this class and its compiles (not calling foo method from anywhere)

javac Demo.java

Why is it Compiling???


Because the only time the foo method will ever be executed will be when it has been called from somewhere. Which means the problem you're worried about cannot possibly occur.

Try and come up with a scenario where that code is run, and where x doesn't have a value. It doesn't exist.

(This whole situation occurs whenever you have a method that takes arguments - it's a pretty fundamental idea).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

I don't know if you come from a C++ background or not. In that language, you CAN set a default value for a parameter.

However, Java is not C.

The compiler considers this to be legal because it knows that the only time this method runs is when it's called by some other method - which must provide a value. the way the language is designed, if they do call it with "foo()", Java will supply an empty array - which is still something.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Vishal Hegde wrote:I am simply compiling this class and its compiles (not calling foo method from anywhere)

javac Demo.java

Why is it Compiling???

Because there is nothing wrong with the code. It's not strange that it compiles. There is no local variable in the code.

Did you read the answers that people wrote above? Did you understand the answers? If not, then what is it that you still don't understand?
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Vishal Hegde wrote:
Vishal Hegde wrote:



I am simply compiling this class and its compiles (not calling foo method from anywhere)

javac Demo.java

Why is it Compiling???


Take this code and try to compile:


Doesn't work does it?
Get it now?
Atul Darne
Ranch Hand

Joined: Jul 05, 2009
Posts: 118

int has its default value initialized to zero
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: local variable not assigned any value in a method yet how does it compile??
 
Similar Threads
Character.MAX_VALUE, ..., not printed
I am confused about the out put of this code
Object points to double array, yet when casting it to single array, it causes Runtime error
parameters/arguments
arrays