Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-11-2008, 03:18 PM   PM User | #1
Tiddler
New to the CF scene

 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Tiddler is an unknown quantity at this point
I have a basic doubt - Java

Hi,

I executed this program without Object and with Object.
With object my answer is 200,202, but I'm not using object ref. I guess,
It should be 18,19. What is the reason ?

// This is my program

package com.test;

public class TestOne {

private static int a=10;
private static int b;

static{
if(a ==10){
a=18;
b=19;
}else{
b=222;
}
}

public TestOne(){
a =200;
b =202 ;
}

public static void main(String[] args) {
TestOne t = new TestOne();
System.out.println(TestOne.a); // Calling without object REF.
System.out.println(TestOne.b);
}
}
Tiddler is offline   Reply With Quote
Old 09-12-2008, 11:28 AM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
because you are calling the constructor which is re-initializing the values

Code:
public static void main(String[] args) {
System.out.println(TestOne.a); // Calling without object REF.
System.out.println(TestOne.b);
}
should give you the expected result
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:39 PM.


Advertisement
Log in to turn off these ads.