Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Output from this java class?
New Posts  All Forums:Forum Nav:

Output from this java class?

post #1 of 4
Thread Starter 
Code:
public class Test {

    String instVar;
    static String statVar = "A";

    Test() {
        System.out.println("test: " + instVar + statVar);
        instVar = "B";
        statVar = "C";
    }

    public static void main(String[] args) {
        Test t1 = new Test();
        t1.instVar = "D";
        t1.statVar = "E";
        Test t2 = new Test();
        System.out.println("inst: " + t1.instVar + t2.instVar);
        System.out.println("stat: " + t1.statVar + t2.statVar);

    }
}
what i'm confused about is the output from these lines:

Code:
Test t2 = new Test();
System.out.println("stat: " + t1.statVar + t2.statVar);
someone care to explain?
My system
(13 items)
 
  
CPUMotherboardGraphicsRAM
[Intel] E7400 @Stock [Intel] DG31PR [EVGA] GTX 460 (1GB Ed.) 4 GB 
Hard DriveOSMonitorPower
[Seagate] 1TB 7200RPM |[WD] 350GB 7200 RPM Windows 7 Ultimate x64 [Philips] 244E 24" 1920x1080 [Ultra]550W LSP 
CaseMouseMouse Pad
[CM] 690 II Adv Logitech G5 Rocketfish 
  hide details  
Reply
My system
(13 items)
 
  
CPUMotherboardGraphicsRAM
[Intel] E7400 @Stock [Intel] DG31PR [EVGA] GTX 460 (1GB Ed.) 4 GB 
Hard DriveOSMonitorPower
[Seagate] 1TB 7200RPM |[WD] 350GB 7200 RPM Windows 7 Ultimate x64 [Philips] 244E 24" 1920x1080 [Ultra]550W LSP 
CaseMouseMouse Pad
[CM] 690 II Adv Logitech G5 Rocketfish 
  hide details  
Reply
post #2 of 4
The object Test has been instantiated 2 times. In the first instance, values for instVar and statVar were manually assigned.

For the second instance (variable t2), no manual values were assigned, hence the default values were used based on the constructor. Which would be B and C. The manually assigned values are D and E.
    
CPUMotherboardGraphicsGraphics
2600k @ 4.6Ghz Biostar TP67XE Powercolor 6870x2 Sapphire 6870 
RAMHard DriveHard DriveHard Drive
4 x 4GB Patriot Viper Xtreme @ 1600 CL8 Intel X-25M 80Gb Hitachi Deskstar 1Tb Seagate 1Tb 
CoolingCoolingMonitorPower
Cogage Arrow 2x Thermalright TY-140 Yamakasi 2703 2560x1440 Seasonic S12D 850w 
Case
Aerocool VS-9 Ghetto Mod 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
2600k @ 4.6Ghz Biostar TP67XE Powercolor 6870x2 Sapphire 6870 
RAMHard DriveHard DriveHard Drive
4 x 4GB Patriot Viper Xtreme @ 1600 CL8 Intel X-25M 80Gb Hitachi Deskstar 1Tb Seagate 1Tb 
CoolingCoolingMonitorPower
Cogage Arrow 2x Thermalright TY-140 Yamakasi 2703 2560x1440 Seasonic S12D 850w 
Case
Aerocool VS-9 Ghetto Mod 
  hide details  
Reply
post #3 of 4
Thread Starter 
That's what I though too kind of. But the complier got nullE for test t2 which confused me like crazy
My system
(13 items)
 
  
CPUMotherboardGraphicsRAM
[Intel] E7400 @Stock [Intel] DG31PR [EVGA] GTX 460 (1GB Ed.) 4 GB 
Hard DriveOSMonitorPower
[Seagate] 1TB 7200RPM |[WD] 350GB 7200 RPM Windows 7 Ultimate x64 [Philips] 244E 24" 1920x1080 [Ultra]550W LSP 
CaseMouseMouse Pad
[CM] 690 II Adv Logitech G5 Rocketfish 
  hide details  
Reply
My system
(13 items)
 
  
CPUMotherboardGraphicsRAM
[Intel] E7400 @Stock [Intel] DG31PR [EVGA] GTX 460 (1GB Ed.) 4 GB 
Hard DriveOSMonitorPower
[Seagate] 1TB 7200RPM |[WD] 350GB 7200 RPM Windows 7 Ultimate x64 [Philips] 244E 24" 1920x1080 [Ultra]550W LSP 
CaseMouseMouse Pad
[CM] 690 II Adv Logitech G5 Rocketfish 
  hide details  
Reply
post #4 of 4
Its natural that it would do that. In your constructor, you are printing the value of instVar before you actually assign a value to it. So the value for instVar at the time of printing is null. Which is expected based on your code.
    
CPUMotherboardGraphicsGraphics
2600k @ 4.6Ghz Biostar TP67XE Powercolor 6870x2 Sapphire 6870 
RAMHard DriveHard DriveHard Drive
4 x 4GB Patriot Viper Xtreme @ 1600 CL8 Intel X-25M 80Gb Hitachi Deskstar 1Tb Seagate 1Tb 
CoolingCoolingMonitorPower
Cogage Arrow 2x Thermalright TY-140 Yamakasi 2703 2560x1440 Seasonic S12D 850w 
Case
Aerocool VS-9 Ghetto Mod 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
2600k @ 4.6Ghz Biostar TP67XE Powercolor 6870x2 Sapphire 6870 
RAMHard DriveHard DriveHard Drive
4 x 4GB Patriot Viper Xtreme @ 1600 CL8 Intel X-25M 80Gb Hitachi Deskstar 1Tb Seagate 1Tb 
CoolingCoolingMonitorPower
Cogage Arrow 2x Thermalright TY-140 Yamakasi 2703 2560x1440 Seasonic S12D 850w 
Case
Aerocool VS-9 Ghetto Mod 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Output from this java class?