Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Accessing instance variable

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By commenting out which line will the following class compile with no errors ?

class ZZY {

transient final int i = 0;
transient final static int j = 10 ;
int k = 78 ;

char jack(){
return 'a';
}

public static void main ( String args [ ]) { // ---> 1
new ZZY ( ) . jack ( ) ; // ---> line 2
int d = new ZZY ( ) . j + new ZZY ( ) . i ; // ---> line 3
new ZZY ( ) . k ; // ---> line 4
System.out.println(newZZY().i+" "+new ZZY().j);//--> line 5
}
}


In this program you have to comment line 4...
new zzy().K; In this line new ZZY() returns object of class zzy and you can access any instance variable as object.varname...
but here why we cant access it?
 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very intersting,

for me, called to the method or accesing directly to a instace variable, its the same.

i will take this on count.!
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code has the same issue, but is simpler:



The second statement is not a valid statement.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Swati Kadam:
...you can access any instance variable as object.varname...
but here why we cant access it?


The reference is fine. The problem is that you're not doing anything with that line, so it's not a statement. If you use this reference in a complete statement, then it's fine. For example...

int x = new ZZY().k;

(This is the same answer I posted under your duplicate.)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic