• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Static Int and Methods

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came acroos this question.
class Q30
{
static int p=abc();
static public int abc()
{

int i=123;
System.out.println("P is " +p);
return i;
}
public static void main(String arg[])
{
int k=Q30.abc();

}
}

In my view the o/p is 123.But when i ran this prg the o/p is 0 123.
Why???
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alpana Singh:
I came acroos this question.
class Q30
{
static int p=abc();
static public int abc()
{

int i=123;
System.out.println("P is " +p);
return i;
}
public static void main(String arg[])
{
int k=Q30.abc();

}
}

In my view the o/p is 123.But when i ran this prg the o/p is 0 123.
Why???



Because the value of p is printed before it is initialized.
[ September 30, 2005: Message edited by: Akshay Kiran ]
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am pretty sure.Just try to run this program.you too will get same o/p
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now one more question

public class initialize
{
public initialize(int m)
{
//System.out.println(m);

System.out.println(i+","+j+","+k+","+x+","+m);
}
public static void main(String[]args)
{

initialize obj=new initialize(x);
}
static int i=5;
static int x;
int j=7;
int k;
{
j=70;x=20;
}
static
{
i=50;
}
}


output is 50 70 0 20 0

Don't know why "m" is 0
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alpana...

This one is for the first piece of code...



Originally posted by Alpana Singh:
I came acroos this question.
class Q30
{
static int p=abc();
static public int abc()
{

int i=123;
System.out.println("P is " +p);
return i;
}
public static void main(String arg[])
{
int k=Q30.abc();

}
}

In my view the o/p is 123.But when i ran this prg the o/p is 0 123.
Why???




Yes you are right about the output..

If you look carefully.....p is a static variable that calls the static method abc()...

When the class is loaded ..first time..it calls the fucntion...abc() to intialize p..In the method..when it encounters the println statement...
and prints the value of p...the result on the console is 0.

Reason p still has the default value of 0. Once it completes the execution of the method it has the value of 123.

Now in the method main() we have a call..

Q30.abc();
Remember that by this time the class level static variable p has the value 123..

Now when you try to print P ...it prints 123...

and so your output for the program is 0 123...

Hope you are answered..

[ September 30, 2005: Message edited by: A Kumar ]



Please dont try to post all questions in the same thread...It would be easy to follow if you can have them in different threads..

I dont have the explanation to you second question...Even i am looking forward for one..


[ September 30, 2005: Message edited by: A Kumar ]
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for solving the problem.

Now about the second prg.

After spending 20 minutes,I thought just becuase the reassignment of x is done in curly braces ,it take the value of x as 20 but when we passed it in the method argument,the xvalue of x is 0.That's why m=0.

Just to check this ,i tried doing by passing i instead of x , and then m=50.

Am I right?
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alpana Singh:
Now one more question

public class initialize
{
public initialize(int m)
{
//System.out.println(m);

System.out.println(i+","+j+","+k+","+x+","+m);
}
public static void main(String[]args)
{

initialize obj=new initialize(x);
}
static int i=5;
static int x;
int j=7;
int k;
{
j=70;x=20;
}
static
{
i=50;
}
}


output is 50 70 0 20 0

Don't know why "m" is 0



See, there are two parts
1. Initialization of class (for static stuff)

12.4.1 When Initialization Occurs
Initialization of a class consists of executing its static initializers and the initializers for static fields declared in the class. Initialization of an interface consists of executing the initializers for fields declared in the interface.
Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized.

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:


T is a class and an instance of T is created.
T is a class and a static method declared by T is invoked.
A static field declared by T is assigned.
A static field declared by T is used and the field is not a constant variable (�4.12.4).
T is a top-level class, and an assert statement (�14.10) lexically nested within T is executed.


2. Initialization of the class (non-static stuff, when the clas is instantiated)


When an object is created, initialization is done in this order: -
Set fields to default initial values (0, false, null) -
Call the constructor for the object (but don't execute the body of the constructor yet) -
Invoke the superclass's constructor -
Initialize fields using initializers and initialization blocks -
Execute the body of the constructor


Now, take a look at your code.
Step 1. takes place as soon as intialize.main(String[]) is entered.
here the values are

i=5
x=0
and then i=50 (static initializer is run)
// j not created yet
// k not created yet

Next, you're instantiating the class initialize with the argument 0 < which is the value of x. x is not passed, just the value of x is passed.

And now, step 2. is executed
i=50 (remains same)
x=0
j=7 (decl)
k=0 (default)
j=70 //instance initializer block is encountered
x=20 //instance initializer block is encountered

so when it returns to the print statement in the constructor, you can see what values will be printed.
while m has the value 0 from the beginning and is never modified.
was that clear?
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

That was a good explanation....

 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clearing the doubt.That' s really a good explanation.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic