Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How do I initialize a class?
New Posts  All Forums:Forum Nav:

How do I initialize a class? - Page 2

post #11 of 14
So you guys are going to teach him 'new' but not 'delete'? Shame on you all. tongue.gif

EDIT: I retract that statement, I didn't see that this was a Java thread, I assumed C++. My fault. tongue.gif
Edited by lordikon - 6/11/11 at 10:59pm
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #12 of 14
Okay I wasn't going to post but I have a few things.

1) Delete is not relevant here, he's coding in java. Same with all the pointer use. The post above mine will probably just confuse you as it is a different language than what you're using.

2) Assuming the classes are all public and they don't have a class relationship (they aren't super/subclasses of eachother) they can be used within themselves if they are in the same package (for your purposes thats pretty much going to mean they are all in the same source or src folder). For Example:

You said you wanted a class A to instantiate a class A (you said restart, in which case I have no idea what you mean) So you have:

Class A{
[constructor(s)]
[method(s)]
public someReturnType someMethod(int someParameter){
new A someVariableName = new A();
//more stuff happens maybe
return someReturnType;
}

So what you have here is just a general method. It returns a type of someReturnType(like int, string, object, it can be any kind of class you've created, void, any many many other things), the method takes an int, and would be called like someA.someMethod(4). Now if you want to reference that 4 within the method, it's called someParameter. Also even though this is within A, it can create an A within it. Same with a B or a C as long as the above mentioned requirements in 2) are met. It can still be done the same way if they're related, but there are more efficient ways to do things like that in those cases.

3) For converting an int to a string, there is a method built into java. Example:

C{
public String intString = " " ;
[constructor(s)]
public C(int anInt){
intString = Integer.toString(anInt);
}
}

Integer is a built in Java type for non-primitive ints. I won't go into primitive data types and stuff if you haven't already gone through it. Also I can't promise my code will compile, just kinda typing it out, but the right ideas are there.

3) The line from a few posts ago "System.out.println(add);" will print out a bunch of junk unless a specific toString method is written, and I'm fuzzy on whether or not out.println calls the toString method of the class or not. It is generally frowned upon to call out.println on a class you've made, when it would be easier to write a method that prints what you want from the class.
Edited by LetThereBeDwight - 6/11/11 at 10:56am
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
post #13 of 14
Thread Starter 
ok everything is done, the only issue I have is that I know I need to have a main class and any number of this kind of class so that I can initialize them.

public class example
{
public void exampleMethod()
{
}
}

my new problem is how do I initialize the main class from a void/method class?

eg I want to load this class:

public class MainClass
{
public static void main(String[]args)
{
System.out.println("I am the main class");
}
}

from this class:
public class MainLoad
{
public void MainLoader()
{
// what would I put here?
}
}
thanks for everyone who has helped so far.
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
Shironeko
(17 items)
 
   
CPUMotherboardGraphicsRAM
i7 860 EVGA P55 SLI EVGA GTX 460 EE 16GB Corsair Vengence CL9 1.5v 
Hard DriveOptical DriveCoolingOS
1xWD 320GB, 2xWD 1TB raid 0, 2x WD 2TB Sata DVD burner Prolimatech Megahalem Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
22" Aoc 2217v 10000:1 5ms G15 V2 Corsair TX750 Lian Li V1000 plus (modded) 
MouseMouse PadAudioAudio
Razer Lachesis V2 Razer Ironclad Razer Orca Headphones Asus Xonar Essence ST (to be modded) 
Audio
Corsair SP2500 2.1 
CPUMotherboardGraphicsRAM
Intel Core 2 duo SL9400 @1.6ghz 1v Lenovo x200T Intel GMA4500x 4GB Samsung 7-7-7-20 1066mhz DDR3 
Hard DriveCoolingOSMonitor
Hitachi 160GB stock Windows 7 Ultimate x86 Resistive Tablet LCD Screen 
PowerCaseMouse
65w Lenovo Power Adapter Lenovo x200T Trackpoint 
CPUGraphicsRAMHard Drive
Dual 2.8Ghz Socket 603/604 Xeons ATI Rage XL 8MB 4GB DDR 266Mhz 6x 36gb 10k rpm SCSI 
CoolingCase
Stock Poweredge 2600 case 
  hide details  
Reply
post #14 of 14
Quote:
Originally Posted by donthaveacow;13840188 
ok everything is done, the only issue I have is that I know I need to have a main class and any number of this kind of class so that I can initialize them.

public class example
{
public void exampleMethod()
{
}
}

my new problem is how do I initialize the main class from a void/method class?

eg I want to load this class:

public class MainClass
{
public static void main(String[]args)
{
System.out.println("I am the main class");
}
}

from this class:
public class MainLoad
{
public void MainLoader()
{
// what would I put here?
}
}
thanks for everyone who has helped so far.

I don't know if you're still looking at this or not, but what you're asking is kind of strange.

First, in the void MainLoader you can say:

MainClass mainClassInstance = new MainClass();

But I'm not sure that it will call the main method, and I know you won't be able to call the main method directly, i.e. mainClassInstance.main(//insert string array here);

But I am confused as to why you would have a main class if it is just going to be instantiated from a different class. A class can instantiate an instance of itself within the main method, and you can call a method to do such as well if necessary. It really depends on what you are actually trying to do. I can't think of a reason you would need a class to load your main class if the main class is going to be the main class for a project or package.
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  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 › How do I initialize a class?