Final non-static data member - java

If you are not allowed to initialize a final non-static data member twice, then how can I set x to something that I want in the following example?
class Temp6
{
final int x;
Temp6()
{
System.out.println(this.x);
this.x=10;
}
public static void main(String[]s)
{
Temp6 t1 = new Temp6();
System.out.println(t1.x);
}
}
Java by default gives x a value of 0, so how can I change it to 10?.

A variable marked final in Java can only be initialized once.
Simply declaring x with final int x; does not initialize it. Therefore, it is legal to assign to x in the Temp6 constructor. However, you would not be able to assign a different value to x after the constructor.
That is, the assignment to t1.x in the following:
public static void main(String[] s) {
Temp6 t1 = new Temp6();
t1.x = 11; // ERROR
}
is not legal.

Initialize final variables in the class constructor.
public class Blam
{
private final int qbert;
public Blam(int qbertValue)
{
qbert = qbertValue;
}
}

Reading this.x in your code should give an error, because final variables are not initialized upon declaration. t1.x should be 10 because x is definitely assigned at the end of the sole constructor.
You have to swap the two lines in the constructor for it to compile and it will be 10 there.
class Temp {
int x; // declaration and definition; defaulted to 0
final int y; // declaration, not initialized
Temp() {
System.out.println(x); // prints 0
x = 1;
System.out.println(x); // prints 1
x = 2; // last value, instance.x will give 2
System.out.println(y); // should be a compiler error: The blank final field y may not have been initialized
y = 3; // definite assignment, last and only value, instance.y will be 3 whereever used
System.out.println(y); // prints 3
y = 4; // compile error: The final field y may already have been assigned
}
}
I never thought about this before, interesting point here.
Final field variables behave like local variables in methods, they must be explicitly assigned before usage (definite assignment is hard to formalize, see JLS reference, but it's quite logical).
If you want to give a value to x from outside, you could do it like this:
public class Temp {
private final int x;
public Temp(int x) {
this.x = x;
}
public int getX() { return this.x; }
public static void main(String[] args) {
Temp temp = new Temp(10);
System.out.println(temp.getX()); // 10
}
}

final variable are java constants. They should be initialized before class loads.
final int x=10;
If your final variable is static then it's not like you have to give the value at the declaration itself, you can have something like -
class Demo {
static final int x;
static {
x = 10;
}
}
static block gets executed only once, at the time of class loading

Related

Data hiding in interface java

interface My{
int x = 10;
}
class Temp implements My{
int x = 20;
public static void main(String[] s){
System.out.println(new Temp().x);
}
}
This prints the result as 20. Is there any way that I can access the x that belongs to the interface in the class?
You need to do an explicit cast to the interface type:
System.out.println(((My)new Temp()).x);
Note however that x is not bound to any instance of My. Interface fields are implicitly static and final (more of constants), meaning the above can be done using:
System.out.println(My.x);
You can always use this.
interface My {
int x = 10;
}
class Temp implements My {
int x = 20;
public static void main(String[] s) {
System.out.println(new Temp().x); // 20
System.out.println(My.x); // 10
}
}
fields of an Interface are always static.

Why can't I see method-local variables outside the method?

I'm new to Java and wanted some clarification, I understand that I'm declaring an int variable of x inside the method's parameters but why is it that 'result' can not be resolved to a variable.
public class Methods{
public static void main(String[] args) {
//f(x) = x * x
square(5);
System.out.println(result);
}
//This method
static int square(int x) {
int result = x * x;
}
You can, but note that local variable are only defined in their respected functions. So even though result is defined in square(), it's not defined in main(). So what you want to do is return a value for your square function and store that in a variable inside main() like so:
public static void main(String[] args) {
int myResult = square(5);
System.out.println(myResult);
}
//This method
static int square(int x) {
int result = x * x; // Note we could just say: return x * x;
return result;
}
Example Here
Since you are beginner, I will explain it bit thoroughly
Rule 1:Local variables are declared in methods, constructors, or blocks.
Rule 2:Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
Rule 3:There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.
public class Methods{
public static void main(String[] args) {
//f(x) = x * x
square(5);
System.out.println(result); //result! who are you?
//Main will not recognize him because of rule 3.
}
static int square(int x) {
int result = x * x; //you did it as said in rule 1
}//variable result is destroyed because of rule 2.
Please go thorough comments in code.
Well solution for your code is:
public class Methods{
public static void main(String[] args) {
//f(x) = x * x
int result=square(5);
System.out.println(result); //result! who are you?
//Main will not recognize him because of rule 3.
}
static int square(int x) {
int result1 = x * x; //you did it as said in rule 1
return result1;
}

Method Local Inner Class Member scope access

How do I access the method variable having the same name as the inner class member instance or method local variable of the inner class?
class A{
int a = 10; //1
public void someMethodA(){
final int a = 20; //2
class B{
int a = 30; //3
public void someMethodB(){
int a = 40; //4
System.out.println("a = "+a); //access 4
System.out.println("a = "+this.a); //access 3
System.out.println("a = "+A.this.a); //access 1
System.out.println(?????); //how do I access value of a i.e 2
}
}
}
}
No. You can't do that. The reason being, the variable a in position 2 is a local variable, which can only be accessed with it's simple name, in the enclosing scope. From JLS §6.4:
A local variable (§14.4), formal parameter (§8.4.1), exception parameter (§14.20), and local class (§14.3) can only be referred to using a simple name (§6.2), not a qualified name (§6.6).
Now, this clears that you can only access that variable with just a. But, you have another variable in method local class B which shadows that local variable a in position 2, which is again shadowed by local variable in position 4.
Now in print statement, a would access the variable from nearmost enclosing scope, that is local variable at position 4. If you remove that variable, it will print the variable at position 3. And then if you remove it, it will print the variable at position 2.
So, the point is, there is no way to access the local variable a at position 2, because that is shadowed.
that code will never output those statements, here is an example from Sun that should explain things :
public class ShadowTest {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
}
}
public static void main(String... args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}
I am not sure, what you actually mean, but you access a class member with the same name as a local variable with the same name like this:
public class A
{
int a = 10;
public void someMethodA()
{
int a = 5;
this.a = 20; //change the member a from 10 to 20
a = 30; // changes the local variable, which is only known in this method to 30
}
}
Typically this pattern is used in constructors, to name the params the same as member variables, for example:
class Foo
{
private int bar = 10;
private string fooBar = 20;
public Foo(int bar, string fooBar)
{
this.bar = bar;
this.fooBar = fooBar;
}
}

Call an instance variable when its name is same with the argument variable

I have this code:
class Foo {
int x = 12;
public static void go(final int x) {
System.out.println(x);
}
}
The argument final x and the instance x have the same name. How would I refer to the instance variable x = 12 if I want to use it in the go() method considering its name is the same with the argument variable?
You need to make it static in order to use it within static method:
static int x = 12;
Then you can get a reference to it via the class name:
public static void go(final int x)
{
System.out.println(Foo.x);
}
Or alternatively, create an instance and use it locally:
int x = 12;
public static void go(final int x)
{
Foo f = new Foo();
System.out.println(f.x);
}
Or use instance method, and refer to the instance x with the keyword this:
int x = 12;
public void go(final int x)
{
System.out.println(this.x);
}
this.x points to the instance variable.
In order to refer to an instance variable, you have to be in a real instance: your method should not be static then.

Java - static initialization

I have written a piece of code :
public class Child{
int y ;
private static final int z = getZ();
static {
System.out.println("The value of z is "+z);
}
public int getX(){
System.out.println("get x");
return 10;
}
public int getY(){
Child ch = new Child();
System.out.println("get y");
ch.y = getX();
return y;
}
public static int getZ(){
System.out.println("get z");
return new Child().getY();
}
public Child(){
System.out.println("Child constructor");
}
public static void main(String...args){
Child ch = new Child();
System.out.println("the value of z in main is "+z);
}
}
And the output is :
get z
Child constructor
Child constructor
get y
get x
The value of z is 0
Child constructor
the value of z in main is 0
Can anyone please explain me why the value of z is 0 and not 10 ?
EDIT:- Thanks everyone , I got the answer to my first question . I still have a doubt , as far as I know the static blocks are executed after the class is loaded and before the first object of the class is instantiated . Well then the SOP("The value of z is "+z) should have been executed before SOP("Child constructor") ! Ain't it ?
Look at getY():
public int getY(){
Child ch = new Child();
System.out.println("get y");
ch.y = getX();
return y;
}
The first three lines are irrelevant - they don't change the value of y in this instance, which is what gets returned.
You're creating an awful lot of pointless objects in frankly spaghetti code, called while initializing the same class that you're constructing instances of. I suggest you try to keep your code a lot simpler than this. Static initializers should be avoided where possible to start with, let alone ones that go all round the houses to do no useful work.
because getY() sets ch.y to 10, but returns the value of this.y.
In GetY you return this.y which never gets set.
On getY() you returned the uninitialized y variable instead of the Child ch instance.
Its because inside getY() method you are constructing a new CHild object and you are assigning 10 to that instance of variable y and when you return y that will be the current instance variable y.
I hope it explains .
To your second point, static fields and static blocks are initialized / executed in textual order.
Eg:
public class StaticTest()
{
static
{
System.out.println("s1 in block 1 :" + s1);
}
private static String s1 = "s1";
static
{
System.out.println("s1 in block 2 : " + s1);
}
public static void main(String args[])
{
new StaticTest();
}
}
The output of this will be -
s1 in block 1 :
s1 in block 2 : s1

Categories

Resources