+ 1

Expain me please what's going on?

class Rock { int age; public static void main(String[] args) { Rock r1 = new Rock(); r1.age=19; System.out.println(r1); } }

3rd Mar 2018, 1:17 PM
Code Race
Code Race - avatar
3 Respostas
+ 2
when u use obj.var its printing the value... when u try to print the obj directly...it prints the address... >> when u have a pre existing class and u hav creayed a object of that type.. supposr.... Classname obj = new Classname(); then the "obj" is not actually tge created object... the "obj" is a pointer to the memory location of that object... so when u print.. System.out.println(r1); it certainly prints the address..
3rd Mar 2018, 3:43 PM
sayan chandra
sayan chandra - avatar
+ 3
You print object rather than object's value which is age. So, output will be memory location of Rock object. If you want to print '19', use println(r1.age) instead of just 'r1'.
3rd Mar 2018, 2:02 PM
Sylar
+ 1
Here age is instance variable So to access instance variable, you have to use object Use "r1.age" instead of "r1" in your code In System.out.println
3rd Mar 2018, 2:10 PM
Ankit Saxena
Ankit Saxena - avatar