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

Output missing something...

 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all...
Can someone explain me why in the below program, the Output
does not print the Char 'V'.
class Process
{
boolean b;
public Process(int x)
{
System.out.println((char)x);
}
}
public class Test
{
Process p3 = new Process('V');

public static void main(String[ ] args)
{
Process p4 = new Process('X');
}
static Process p1 = new Process('J');
static Process p2 = new Process('$');
}
The Output of the Above Prog:-
J
$
X
Why doesn't the Char 'V' is not included in Output ?
Thanks.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shah Chunky:
Hello all...
Can someone explain me why in the below program, the Output
does not print the Char 'V'.
public class Test
{
Process p3 = new Process('V');

public static void main(String[ ] args)
{
Process p4 = new Process('X');
}
static Process p1 = new Process('J');
static Process p2 = new Process('$');
}
The Output of the Above Prog:-
J
$
X

In your Test class the variable p3 is an instance variable so it is never created until you create an actual Test object. The variables p1 and p2 are static so they are created and instantiated when the Test class is loaded. Then in main you specifically create a Process object so it too prints. To see the 'V' printed, just create an instance of your Test class in main.
add a line like this:
Test newTest = new Test();
and then V will print.
hth
Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic