• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Doubt in Output of the following code

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

how come in this code there is no compiler error at line 2. It gives me a compiler error at line 4. Both are not a valid statement right.

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 ( new ZZY ( ) . i + " " + new ZZY ( ) . j ) ; // ---> line 5
}
}
Options :

a . line 1
b . line 2
c . line 3
d . line 4
e . line 5

Thanks
Srividhya
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 4 is not valid, because the statement here lacks an assignment operator (=). Which is really the only reason why this statement is illegal, and the statement at line 3 is legal.
The statement at line 2 does not require assignment, because it's a method call. So by commenting out line 4 (or by adding assignment logic), this code will compile just fine.
Also, don't let all the white spaces throw you off, it's perfectly legal syntax.
[ March 21, 2008: Message edited by: Jelle Klap ]
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic