Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › What's the difference between these two object creations in Java?
New Posts  All Forums:Forum Nav:

What's the difference between these two object creations in Java?

post #1 of 4
Thread Starter 
Quote:
class Dog{}

class Pet{} {
private Dog dog1; //Creation 1
Dog dog1 = new Dog(); //Creation 2
}

What's the difference between 1 and 2?
Gaming Beast
(17 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i5 4430 MSI Z87-G41 PC Mate MSI GeForce GTX 660 Twin Frozr OC Corsair Value Select 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 840 Western Digital Caviar Green Seagate Barracuda LiteOn IHAS124-14 24x SATA Internal DVD-RW / DV... 
OSMonitorMonitorKeyboard
Windows 8.1 22" Samsung S22A200B Widescreen LED Monitor Iiyama PLX436S 17" Microsoft SIdewinder X4 
PowerCaseMouseMouse Pad
450W Coolermaster GX 80 Plus EZCOOL Mesh Design Mid Tower Case Razr DeathAdder Razr Goliathus 
Audio
M-Audio Audiophile 2496 
CPUMotherboardGraphicsOS
i7 4700HQ MSI MS-16H2 Intel(R) HD Graphics 4600 Microsoft Windows 8.1 
KeyboardPowerAudio
Steelseries 6-Cell Li-Polymer(4840mAh 52Wh) Sound by Dynaudio, Support 7.1 channel S/PDIF o... 
  hide details  
Reply
Gaming Beast
(17 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i5 4430 MSI Z87-G41 PC Mate MSI GeForce GTX 660 Twin Frozr OC Corsair Value Select 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 840 Western Digital Caviar Green Seagate Barracuda LiteOn IHAS124-14 24x SATA Internal DVD-RW / DV... 
OSMonitorMonitorKeyboard
Windows 8.1 22" Samsung S22A200B Widescreen LED Monitor Iiyama PLX436S 17" Microsoft SIdewinder X4 
PowerCaseMouseMouse Pad
450W Coolermaster GX 80 Plus EZCOOL Mesh Design Mid Tower Case Razr DeathAdder Razr Goliathus 
Audio
M-Audio Audiophile 2496 
CPUMotherboardGraphicsOS
i7 4700HQ MSI MS-16H2 Intel(R) HD Graphics 4600 Microsoft Windows 8.1 
KeyboardPowerAudio
Steelseries 6-Cell Li-Polymer(4840mAh 52Wh) Sound by Dynaudio, Support 7.1 channel S/PDIF o... 
  hide details  
Reply
post #2 of 4
Quote:
Originally Posted by leekaiwei View Post

Quote:
class Dog{}
class Pet{} {
private Dog dog1; //Creation 1
Dog dog1 = new Dog(); //Creation 2
}
What's the difference between 1 and 2?

It's been a while for me, but I believe on //Creation 1, it hasn't actually be instantiated yet, you've only assigned it a name. In order to instantiate the object, you'd still need to do:

private Dog dog1;
dog1 = new Dog();

For //Creation 2, you've combined the two into a single line (sans the scope declaration):

Dog dog1 = new Dog();

So:

private Dog dog1;
dog1 = new Dog();

Would be the same as:

private Dog dog1 = new Dog();
Edited by Warhaven - 12/8/11 at 2:14pm
post #3 of 4
Quote:
Originally Posted by Warhaven View Post

It's been a while for me, but I believe on //Creation 1, it hasn't actually be instantiated yet, you've only assigned it a name. In order to instantiate the object, you'd still need to do:
private Dog dog1;
dog1 = new Dog();
For //Creation 2, you've combined the two into a single line (sans the scope declaration):
Dog dog1 = new Dog();
So:
private Dog dog1;
dog1 = new Dog();
Would be the same as:
private Dog dog1 = new Dog();


^This. And just to add on. When you

private Dog dog1 = new Dog();

new Dog(); is calling constructor the the object you created. It's useful for giving the object (or dog in this case) attributes when you declare it. Like...
Code:
private Dog dog1 = new Dog(weight, stringType, stringSex);

/* ***************************** */

class Dog
{
     private int weight;
     private String Type;
     private String Sex;

     public Dog(int x, string y, string s)
     {
          weight = x;
          Type = y;
          Sex = s;
     }
}

So, if you define the constructor for the object you created with parameters (like the weight, type, sex) can set those at the time of creation for your object. Or alternatively you can set them later in functions (or both!)

Hope that made sense.... redface.gif
Edited by ColSanderz - 12/8/11 at 2:45pm
Flash Penguin
(20 items)
 
Audio Stuffs
(9 items)
 
FileSerpent
(10 items)
 
CPUMotherboardGraphicsGraphics
Xeon W3520 EVGA X58 Classified GTX 275 GTX 275 
GraphicsRAMHard DriveHard Drive
GT 640 G.Skill Trident 1710 Mhz @ 6.7.6.18 Vertex 2 Seagate 1tb 7200.12 
Optical DriveCoolingCoolingCooling
LG Multi-DVD Writer Apogee XT Bitspower mobo and mosfet blocks for EVGA e760 Feser 360 and Feser 240 
OSMonitorKeyboardPower
Win7 x86-64 3x LG W2353V 23" 1080p G15 Gaming Keyboard (V1) [BFG] ES-800 
CaseMouseMouse PadAudio
Antec 1200 LED/Window/HDD rack Modded Logitech G700 An old mousepad (shows a computer with ISA slots) Asus Essence ST 
AudioAudioAudioAudio
Rega Saturn-R CDP/DAC Rega Elicit-R Integrated Amplifier Vandersteen Audio Treo Four-Way, Ported Floor-S... Rega RP8 w/ Exact2 cartridge 
AudioAudioAudioAudio
Cambridge Audio Stream Magic 6 Network Player Beyerdynamic Tesla T1 Headphones reterminated t... Sennheiser HDVA 600 Rega DAC 
Audio
Nordost QB4 w/ Nordost Heimdall Norse 2 Mains C... 
CPUMotherboardRAMHard Drive
Xeon E3-1240 V3 ASROCK E3C226D2I 2x8GB 1333 Kingston Unbuffered ECC 2x WD Black 500 GB in Raid 1 
Hard DriveCoolingOSPower
8x WD Red 3TB in Raid 10 Noctua NH-U9B Windows Server 2012 x64 Seasonic SS-400FL2 400w 
CaseOther
iStarUSA S-917 Adaptec 8885 Raid Controller 
  hide details  
Reply
Flash Penguin
(20 items)
 
Audio Stuffs
(9 items)
 
FileSerpent
(10 items)
 
CPUMotherboardGraphicsGraphics
Xeon W3520 EVGA X58 Classified GTX 275 GTX 275 
GraphicsRAMHard DriveHard Drive
GT 640 G.Skill Trident 1710 Mhz @ 6.7.6.18 Vertex 2 Seagate 1tb 7200.12 
Optical DriveCoolingCoolingCooling
LG Multi-DVD Writer Apogee XT Bitspower mobo and mosfet blocks for EVGA e760 Feser 360 and Feser 240 
OSMonitorKeyboardPower
Win7 x86-64 3x LG W2353V 23" 1080p G15 Gaming Keyboard (V1) [BFG] ES-800 
CaseMouseMouse PadAudio
Antec 1200 LED/Window/HDD rack Modded Logitech G700 An old mousepad (shows a computer with ISA slots) Asus Essence ST 
AudioAudioAudioAudio
Rega Saturn-R CDP/DAC Rega Elicit-R Integrated Amplifier Vandersteen Audio Treo Four-Way, Ported Floor-S... Rega RP8 w/ Exact2 cartridge 
AudioAudioAudioAudio
Cambridge Audio Stream Magic 6 Network Player Beyerdynamic Tesla T1 Headphones reterminated t... Sennheiser HDVA 600 Rega DAC 
Audio
Nordost QB4 w/ Nordost Heimdall Norse 2 Mains C... 
CPUMotherboardRAMHard Drive
Xeon E3-1240 V3 ASROCK E3C226D2I 2x8GB 1333 Kingston Unbuffered ECC 2x WD Black 500 GB in Raid 1 
Hard DriveCoolingOSPower
8x WD Red 3TB in Raid 10 Noctua NH-U9B Windows Server 2012 x64 Seasonic SS-400FL2 400w 
CaseOther
iStarUSA S-917 Adaptec 8885 Raid Controller 
  hide details  
Reply
post #4 of 4
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › What's the difference between these two object creations in Java?