• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 119
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the value is needed here which should be passed by the calling function
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x is not a local variable, it's a method argument.

Do you think this would give you an error ?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 119
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int has its default value initialized to zero
reply
    Bookmark Topic Watch Topic
  • New Topic