More Java questions

Community Forums/Technical Discourse/More Java questions

loofadawg(Posted 2017) [#1]
I am confused by the output:


class Test1 {
   int var = 1;
   Test1() {
     var = 2; 
     System.out.println(var);
   }
}

class Test2 {
   int var = 1;
   Test2() {
     int var = 2; 
     System.out.println(var); 
   }
}



class MyClass {
    public static void main(String[ ] args) {
        Test1 myTest1 = new Test1();     // displays 2
        System.out.println(myTest1.var); // displays 2
        
        System.out.println();
        
        Test2 myTest2 = new Test2();     // displays 2
        System.out.println(myTest2.var); // displays 1
    }
}



Results:

2
2

2
1



xmlspy(Posted 2017) [#2]
You are declaring an integer var in Test2 constructor which is used in the println. That var in the constructor is not the same var as the field/member of Test2 class. This is called "shadowing" -- the scopes of the variables are different.

http://javabeginnerstutorial.com/core-java-tutorial/variable-shadowing/


loofadawg(Posted 2017) [#3]
Thanks for the link xmlspy.


loofadawg(Posted 2017) [#4]
I get it now. Test1 has 1 variable named var.
Test2 has 2 with same name but are accessed differently through the
this keyword.


class Test1 {
   int var = 1;
   Test1() {
     var = 2; 
     System.out.println(this.var);
     System.out.println(var);
   }
}

class Test2 {
   int var = 1;
   Test2() {
     int var = 2; 
     System.out.println(this.var); 
     System.out.println(var);
   }
}



class MyClass {
    public static void main(String[ ] args) {
        Test1 myTest1 = new Test1();     // displays 2 then 2
        System.out.println(myTest1.var); // displays 2
        
        System.out.println();
        
        Test2 myTest2 = new Test2();     // displays 1 then 2
        System.out.println(myTest2.var); // displays 1
    }
}


Displays:
2
2
2

1
2
1



loofadawg(Posted 2017) [#5]
By the way... the SoloLearn app is really good. But having the blitz community is just icing on the cake when it comes to self learning.


Pakz(Posted 2017) [#6]
Yeah, nice thread this is. I stored the code from here as I had not really learned this yet.


loofadawg(Posted 2017) [#7]
Hey Pakz. You messed with 2D cameras in platform games yet ? I was working with a lerp function and a push zone window inside the viewport for that Super Mario World feel. I deviate from my side projects to dip my toe back into Java and Monkey.

I like a lot of your examples which I have come across. I find a lot of my stuff mirrors your stuff but generally you are ahead of the curve. I will get some stuff up here someday. I get feeling all inferior to what everyone else can do but I think I am coming to grips that it's okay. My learning can only get better.


loofadawg(Posted 2017) [#8]
Oh... your blog I would love to view it but on my tablet it formats everything awfully. Just long long long lines of horizontal text.


Pakz(Posted 2017) [#9]
I am not that familiar with camera's. I just scroll a pixel at a time. I have not found or looked for camera stuff. My old regular aproach seems to work.

Getting experienced requires a lot of hours doing the right things. It took me years being able to make platformer games like those from the early ninetees. Nowadays there is a lot more information online that should make it easier to learn how to do it.

As for my blog. I also have all the code from those on my github. I put it there last year. It is only the code files but almost everyting should compile straight away without problems.(except that old java stuff)

https://github.com/pakz001