• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static and final  RSS feed

 
Pooja Pawar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Will above code compile from static and final keyword usage point of view and why ?
Can static method use non static local variables?
What is the impact of final keyword here (apart from making value of x not changeable).
Thnks
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Did you try to compile that code? What happened when you tried?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this looks like homework. We encourage everyone here to do their own homework.
 
Pooja Pawar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I compiled it except main method is missing no other error was thrown.
I was unable to understand why it compiled correctly so I pasted the code here.
I agree its a exercise question but I was unable to understand why it compiled correctly so I pasted it here.
As per my understanding static method should not accept non static instance variable.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to post code, please UseCodeTags (<-- click that, it's a link)

Also, please post the actual code that you compiled so everyone is on the same page. We take code very literally, as does the Java compiler, and the code you posted actually will not compile as is.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pooja Pawar wrote:As per my understanding static method should not accept non static instance variable.

Which line of code are you referring to by that? There is nothing in that code you gave that calls the go() method. The parameter x declared in the go() method signature is different from the instance variable x defined above it. The System.out.println(x) statement is displaying the value of the parameter, not the instance variable.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pooja Pawar wrote:. . . Can static method use non static local variables?
What is a non static local variable? How can a local variable be static or not?
Don't say non static; say instance.
What is the impact of final keyword here (apart from making value of x not changeable).
Thnks
What other impact would the final keyword have?

As Junilu has told you, you have two variables called x.

And welcome to the Ranch again
 
Pooja Pawar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have compiled this code.
 
Pooja Pawar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parameter x declared in the go() method signature is different from the instance variable x defined above it. The System.out.println(x) statement is displaying the value of the parameter, not the instance variable.


Thanks for the reply. I am clear on this now

 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so do you understand why it gives the output of 3?

By the way, your code formatting is very bad. If you are not using an IDE that can automatically format your code for you, there are online code beautifiers you can use. Here's one: http://www.freecodeformat.com/java-format.php

Next time, please format your code properly so people don't have a hard time reading your code.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!