• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variable within static method

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 doubts:

1) Given this code:


The result is 10, but I'd say 70. The problem I have with the method zoomMore.
Why the change of the static variable magnify remains, so to speak, within that method? We do not speak of a local variable.



2) Given this code:


The compile error is related to the attempt to change an instance variable declared final, of course, is not possible.
But I would have thought of a mistake in filling out this line:


as you try to use through the "this" keyword to a static variable.
This line does not generate error for the same reason for which no error generates a call to a static method using an object rather than the name of the class?

 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
The result is 10, but I'd say 70. The problem I have with the method zoomMore.
Why the change of the static variable magnify remains, so to speak, within that method? We do not speak of a local variable.


The zoomMore() method has a local variable with the same name as the magnify static variable -- why don't you want to speak of it?

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:The result is 10, but I'd say 70. The problem I have with the method zoomMore.
Why the change of the static variable magnify remains, so to speak, within that method? We do not speak of a local variable.

The parameter of the zoomMore method has exactly the same name as the static/class variable. So you are multiplying the local variable, not the class/static variable. If you want to multiply the class/static variable you have to qualify using the class name as shown in this code snippet

John Lerry wrote:This line does not generate error for the same reason for which no error generates a call to a static method using an object rather than the name of the class?


You are spot-on!

Hope it helps!
Kind regards,
Roel
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
as you try to use through the "this" keyword to a static variable.
This line does not generate error for the same reason for which no error generates a call to a static method using an object rather than the name of the class?



Not sure what you are trying to ask. Are you asking if you can access a static field with a reference? If so, then the answer is "yes".

Henry
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John Lerry wrote:
The result is 10, but I'd say 70. The problem I have with the method zoomMore.
Why the change of the static variable magnify remains, so to speak, within that method? We do not speak of a local variable.


The zoomMore() method has a local variable with the same name as the magnify static variable -- why don't you want to speak of it?

Henry


I considered as local variables ONLY those defined within the body of a method and not those defined as parameters of a method. The doubt was there.
but being a local variable should not be initialized in some way?
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
I considered as local variables ONLY those defined within the body of a method and not those defined as parameters of a method. The doubt was there.
but being a local variable should not be initialized in some way?


Method parameters are variables, and they are local to the method, hence, they are local variables. Also, try to declare a local variable with the same name as a parameter; you will notice that you can't.

As for what they are initialized with, well, isn't that obvious?

Henry
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is just about the initialization of the local variables declared as parameters of methods.
Being their local variables should be compulsorily initialized in the declaration which of course you can not do within the parameter list of a method.
Also in this case we have the statement:


I do not understand what to return. ok, it's like:


but what value contains the local variable magnify seen that there is no explicit initialization?
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Oh, you were actually serious? I thought you were joking that parameters weren't initialized... ... sorry.



Parameters are *always* initialized. And they are initialized with the value that was passed in by the caller. The compiler never needs to check the method code to confirm initialization of local variables (that are parameters).

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:Being their local variables should be compulsorily initialized in the declaration which of course you can not do within the parameter list of a method.

It's not required to initialize a local variable in its declaration. The only requirement is that you have to initialize a local variable before you use the variable. This code compiles without errors:At line2 the local variable i is used for the very first time. So the local variable i must be initialized before this line, otherwise you'll get a compiler error. It's initialized on line1, so that's ok. The local variable s is not used in the code, so it's not required to initialize this local variable.

John Lerry wrote:The problem is just about the initialization of the local variables declared as parameters of methods.

Java doesn't know the concept of "optional parameters" like in other programming languages, so a parameter is always initialized with the value you pass in when invoking the method. ExampleWith this invocation the parameter magnify will have 15 as value (which can easily be verified by adding a print statement like in this code snippet

Hope it helps!
Kind regards,
Roel
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Java doesn't know the concept of "optional parameters" like in other programming languages, so a parameter is always initialized with the value you pass in when invoking the method.


I too, like the concept of "optional parameters" -- although admittedly, since Java supports method overloading, it is arguably just syntactic sugar. Pretty good sugar, but sugar nonetheless...

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic