aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes explanation needed Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "explanation needed" Watch "explanation needed" New topic
Author

explanation needed

Suhita Reddy
Ranch Hand

Joined: Jun 09, 2006
Posts: 60
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

Joined: Aug 31, 2004
Posts: 11343

System.out.println("1 1 1");



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ankur Sharma
Ranch Hand

Joined: Dec 27, 2005
Posts: 1234
Great,

This is an invisible code:

wow Mark how do you able to see the Code:

Below is the Solution:





Please explain this problem..
Ankur Sharma
Ranch Hand

Joined: Dec 27, 2005
Posts: 1234
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

Joined: Oct 02, 2003
Posts: 9956
    
    6

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!!!


Never ascribe to malice that which can be adequately explained by stupidity.
Suhita Reddy
Ranch Hand

Joined: Jun 09, 2006
Posts: 60
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

Joined: Feb 07, 2005
Posts: 2341
Are you sure that is the correct code?

That code doesn't output what you said.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
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

Joined: Jul 08, 2003
Posts: 24061
    
  13

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: explanation needed
 
Similar Threads
What is your answer?
shift operators
forward referencing
question on maximum value of primitive data type
Result of the Code