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?

post #1 of 14
Thread Starter 
Hello.
Ok so I'm trying to initialize a class in java from within a class so say I have class A, class B and class C, and I want to restart class A from within class A, or I was to say initialize class C from within class B, is there a way to do it, if so how?

again, any help would be great!
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 #2 of 14
Well the way to use a class within a class would be:

Class_C hello = new Class_C();

Where Class C is the name of the class you want to use, "hello" would be the name you want to assign the class you are using and new Class_C() would be the constructor of the class. Say I made an addition class whose constructor took 2 numbers that needed to be added and I wanted to add the numbers 3 and 4:

additionClass add = new additionClass(3,4);
System.out.println(add);

This would print "7".

I sincerely apologize if this does not help you out, I have had a few drinks tonight haha.
post #3 of 14
Thread Starter 
Quote:
Originally Posted by GBob314 View Post
Well the way to use a class within a class would be:

Class_C hello = new Class_C();

Where Class C is the name of the class you want to use, "hello" would be the name you want to assign the class you are using and new Class_C() would be the constructor of the class. Say I made an addition class whose constructor took 2 numbers that needed to be added and I wanted to add the numbers 3 and 4:

additionClass add = new additionClass(3,4);
System.out.println(add);

This would print "7".

I sincerely apologize if this does not help you out, I have had a few drinks tonight haha.
hmmm, I think you might be able to answer my other threads question.

can you give me an example of the other class that would add the 2 numbers, or if possible use whatever is within the brackets of Class_C hello = new Class_C(); and then copy that over into a string?

also I noticed that it returns a "missing return statement", do I have to use a method (public void..... for example)?
Edited by donthaveacow - 6/11/11 at 3:03am
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 #4 of 14
Well a very inefficient way of turning an int into a String would be to make a class like this haha

Code:
public class toString {

String string = null;

public toString(int i){
this.string = i + "";
}

public String getString(){
return(string);
}
}
So basically if I wanted to make the number 3 a string with this class I would do this:

toString convert = new toString(3);
System.out.println(convert.getString());

So I am using the constructor method to turn the 3 into a string (assigning the variable "string" within the toString class to 3) and then using an accessor method to return the value of string within the toString class.

...once again, I apoligize if I end up confusing you more than when you came in here.


EDIT: Your missing return statement might be because you are missing a return type on a method.

public String toString();

notice the "String" part is what data type the method will be returning. IE: if you wanted to do some math with a method you would return an integer, right? so you would use "public int doSomeMath(int i)"
Edited by GBob314 - 6/11/11 at 3:11am
post #5 of 14
Thread Starter 
oh as in, how would I do it say with a string instead of numbers, (not as in converting, just any string). and say I had a string in class B and I wanted to use that string in class c but I dont want class b to ask for a return value from c
so say,

class b
{
yada yada
{
String example = new String("im an example");
}
}

and I wanted to initialize class c and use the string 'example' for some purpose.

eg
System.out.println(
//and then I would put the string 'example' there.
);
Edited by donthaveacow - 6/11/11 at 3:18am
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 #6 of 14
So if "example" was a variable within class b you would have to access it like this:

b B = new b();
String classC = B.example;

You initialize class b first and then access "example" like that.
post #7 of 14
Thread Starter 
Quote:
Originally Posted by GBob314 View Post
So if "example" was a variable within class b you would have to access it like this:

b B = new b();
String classC = B.example;

You initialize class b first and then access "example" like that.
thanks, that solves one of my issues, now I just need to figure out how to initialize class c from within class b without needing a method/having to return something.
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 #8 of 14
Quote:
Originally Posted by donthaveacow View Post
thanks, that solves one of my issues, now I just need to figure out how to initialize class c from within class b without needing a method/having to return something.
I believe you cannot access a class without first invoking its constructor.

I've only taken one semester of programming so I could be wrong, just helping with what little knowledge I have . And being slightly intoxicated probably isn't helping out very much... haha
post #9 of 14
Thread Starter 
Quote:
Originally Posted by GBob314 View Post
I believe you cannot access a class without first invoking its constructor.

I've only taken one semester of programming so I could be wrong, just helping with what little knowledge I have . And being slightly intoxicated probably isn't helping out very much... haha
haha thank you very much though, anyway yea I'm making a program which is roughly 1,000 lines of code and I have 6 separate classes, so I would like to try and link all the classes without having to make methods unnecessarily.
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 #10 of 14
Quote:
Originally Posted by donthaveacow View Post
Hello.
Ok so I'm trying to initialize a class in java from within a class so say I have class A, class B and class C, and I want to restart class A from within class A, or I was to say initialize class C from within class B, is there a way to do it, if so how?

again, any help would be great!
Code:
class B {
  int bValue;
  B() { C cClass = new C(); bValue = 0; } 
}
When you instantiate the class B, it's constructor would be creating an instance of class C as well.

If you look at the constructor for class B, the first statement in there creates an instance of class C (also known as creating an object).
Primary PC
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 720 (2.8 ghz) BIOSTAR A780L3L (AM3) MSI GeForce 9800GT G.Skill Ripjaws Series DDR3 1600 (8GB) 
Hard DriveOSMonitorPower
Crucial M4 64GB / Samsung F3 1TB Windows 7 Pro (64-bit) Dell SP2309W (2048x1152) PC Power & Cooling 610W 
CaseMouse
Cooler Master Elite 430 RC Logitech MX518 
  hide details  
Reply
Primary PC
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 720 (2.8 ghz) BIOSTAR A780L3L (AM3) MSI GeForce 9800GT G.Skill Ripjaws Series DDR3 1600 (8GB) 
Hard DriveOSMonitorPower
Crucial M4 64GB / Samsung F3 1TB Windows 7 Pro (64-bit) Dell SP2309W (2048x1152) PC Power & Cooling 610W 
CaseMouse
Cooler Master Elite 430 RC Logitech MX518 
  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?