Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explanation needed

 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,



wht is the o/p of this?can u explain how the o/p comes?
ans is 1 1 1
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("1 1 1");

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great,

This is an invisible code:

wow Mark how do you able to see the Code:

Below is the Solution:





Please explain this problem..
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc,

I think we should now start an Invisible Code Forum too..


And definately I will suggest your name as a bartender for that forum.

Pls start that as early as possible.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's called Whitespace, and it is a valid, if somewhat difficult programming language.

ok, but seriously, Suhita, if you want to post the code, we'd all love to help!!!
 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry to all,

this is the code

public class T051 extends Z
{
protected static int x = 0; //1

public static void main(String args[])
{
Z z = new T051();

System.out.println(z.x); //2
}
}

class Z
{
private static int x = 1; //3
}//2
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that is the correct code?

That code doesn't output what you said.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output is "1", not "1 1 1". The single println() in main() prints the value of Z.x because the compiler figures out what variable to access during compilation based on the compile-time type of the variable "z". There's no runtime dynamic lookup. Variables (static or not) are never polymorphic.

This is an SCJP question, and as a matter of policy, these get moved to the SCJP forum.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I mean is there is a compile-time error in that code. I just wondered if they posted the wrong one.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:
What I mean is there is a compile-time error in that code. I just wondered if they posted the wrong one.


I posted my reply without seeing yours first. But you're right -- Z.x is not accessible in T051.main() because it's private. My answer only holds if Z.x were accessible.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic