Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Set variable equal to a method return in Java
New Posts  All Forums:Forum Nav:

Set variable equal to a method return in Java

post #1 of 18
Thread Starter 
I'm very new to java, so this may seem very basic. I'm trying to set a variable in my main method equal to a value returned from a method in a separate class. However, Eclipse is giving me an error.
Quote:
Cannot make static reference to non-static method
The offered quick fix is to make the method that I'm refering to static. I'm not 100% sure what this means.

Main method class
Code:
public class StartProject 
{
public static void main(String[] args) 
{
int sum3;
int sum5;

sum3 = Add3.run();
sum5 = Add5.run();

System.out.println(sum3 + sum5);
}
}

Add3 class (same as Add5 class, except i=5 instead of 3. I know, inefficient, but I'll fix that later)
Code:
public class Add3 
{
int result;
int run()
{
int i=3;
while(i<1000)
{
result = result+i;
i=i+3;
}

return result;
}
}

So it's asking me to make run() static. Will this harm anything? Is this good or bad practice? Is there a better way? Like I said, I'm super new to java. I keep starting, and not having the time to continue, so I stop.
    
CPUMotherboardGraphicsRAM
Phenom IIx4 955 @3.2ghz 1.285V Asus M4A79XTD EVO 6950 @ 850/1250 4x2GB DDR3 
Hard DriveOptical DriveOSMonitor
Hitachi 1Tb + Seagate 1.5Tb Samsung super speed Windows 7 Pro(games)/Linux Mint 10 (not games) LP2065 + 2x 2007FPb in IPS Eyefinity 4800*1200 
KeyboardPowerCaseMouse
Logitech Wave Corsair HX620 Azza Solano 1000 Logitech MX revolution 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Phenom IIx4 955 @3.2ghz 1.285V Asus M4A79XTD EVO 6950 @ 850/1250 4x2GB DDR3 
Hard DriveOptical DriveOSMonitor
Hitachi 1Tb + Seagate 1.5Tb Samsung super speed Windows 7 Pro(games)/Linux Mint 10 (not games) LP2065 + 2x 2007FPb in IPS Eyefinity 4800*1200 
KeyboardPowerCaseMouse
Logitech Wave Corsair HX620 Azza Solano 1000 Logitech MX revolution 
  hide details  
Reply
post #2 of 18
Java is object oriented. That means that whatever methods you call on have to be from an instance of an object. You can get past that by making a method static. This means that the method will be the same for every instance of that class.
Code:
public class StartProject 
{
public static void main(String[] args) 
{
int sum3;
int sum5;
                Add3 addthree = new Add3();
                Add5 addfive = new Add5();

sum3 = addthree.run();
sum5 = addfive.run();

System.out.println(sum3 + sum5);
}
}

Here, we create instances of the Add3 and Add5 class, then we call those methods through the objects we created.
Edited by Jtvd78 - 6/9/11 at 1:31pm
post #3 of 18
Quote:
Originally Posted by E_man;13812308 
I'm very new to java, so this may seem very basic. I'm trying to set a variable in my main method equal to a value returned from a method in a separate class. However, Eclipse is giving me an error. The offered quick fix is to make the method that I'm refering to static. I'm not 100% sure what this means.

Main method class
Code:
public class StartProject 
{
public static void main(String[] args) 
{
int sum3;
int sum5;

sum3 = Add3.run();
sum5 = Add5.run();

System.out.println(sum3 + sum5);
}
}

Add3 class (same as Add5 class, except i=5 instead of 3. I know, inefficient, but I'll fix that later)
Code:
public class Add3 
{
int result;
int run()
{
int i=3;
while(i<1000)
{
result = result+i;
i=i+3;
}

return result;
}
}

So it's asking me to make run() static. Will this harm anything? Is this good or bad practice? Is there a better way? Like I said, I'm super new to java. I keep starting, and not having the time to continue, so I stop.

Simple fix, either make the method a static method. Or insantiate an instance of your class where you're calling your method.

When you make a method or variable static, it means that it can be used without creating an object; meaning you can use that method or variable without having to go through the trouble of creating an entire class. It's a decent space saving technique; and many of the Java core libraries require the use of the 'static' modifier.

For such a simple program, making the method static would be ok, but it is generally perfered that you create the object using something like:
Code:
MyObject myObject = new MyObject();

I'm on my phone but that would be the general way to create a new object of type MyObject. Then to call a method from your object, and assign it to a variable you could use:
Code:
myVariable = myObject.myMethod();

Edited by CovertCover - 6/9/11 at 1:48pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
post #4 of 18
Thread Starter 
Thanks. Definitely some work getting used to this. All my experience has been in C. Nothing I've done is objects like this. This worked great.
    
CPUMotherboardGraphicsRAM
Phenom IIx4 955 @3.2ghz 1.285V Asus M4A79XTD EVO 6950 @ 850/1250 4x2GB DDR3 
Hard DriveOptical DriveOSMonitor
Hitachi 1Tb + Seagate 1.5Tb Samsung super speed Windows 7 Pro(games)/Linux Mint 10 (not games) LP2065 + 2x 2007FPb in IPS Eyefinity 4800*1200 
KeyboardPowerCaseMouse
Logitech Wave Corsair HX620 Azza Solano 1000 Logitech MX revolution 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Phenom IIx4 955 @3.2ghz 1.285V Asus M4A79XTD EVO 6950 @ 850/1250 4x2GB DDR3 
Hard DriveOptical DriveOSMonitor
Hitachi 1Tb + Seagate 1.5Tb Samsung super speed Windows 7 Pro(games)/Linux Mint 10 (not games) LP2065 + 2x 2007FPb in IPS Eyefinity 4800*1200 
KeyboardPowerCaseMouse
Logitech Wave Corsair HX620 Azza Solano 1000 Logitech MX revolution 
  hide details  
Reply
post #5 of 18
Quote:
Originally Posted by CovertCover;13812622 
it is generally perfered that you create the object using something like:
Code:
MyObject myObject = new MyObject();

+1

Exactly. thumbsupsmiley.png
post #6 of 18
Im learning java as well and ive only seen the run() as a static or instance method for threads in games developed in java. in this case the int run() what does that actually do? Im going to print it lol and see what it gives me, also what are the Addn = new Addn() is this a built in class that will add n to another number? I have never seen this before, if you dont mind can someone please explain this to me.
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #7 of 18
Quote:
Originally Posted by Sin Cities Sin;13856311 
Im learning java as well and ive only seen the run() as a static or instance method for threads in games developed in java. in this case the int run() what does that actually do? Im going to print it lol and see what it gives me, also what are the Addn = new Addn() is this a built in class that will add n to another number? I have never seen this before, if you dont mind can someone please explain this to me.

ok, c++ and java are object-oriented, but are slightly different in the syntax in terms of creating an object.

For java...lets say the class name is Dog
Code:
Dog scruffy = new Dog();
now you just made a Dog object called scruffy. You can now call all of the methods from the Dog class like this...in main or other classes.
Code:
scruffy.weight();
scruffy.breed();
scruffy.gender();

in c++ it is slightly different...but the same concept is applied.
Code:
int run(){

}
this is just a method that returns an integer data type. You must have the keyword "return" followed by an integer or variable that holds an integer inside the method.
Edited by surfbumb - 6/13/11 at 3:34pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #8 of 18
i know all about oop in java how do you create and destroy an object in c++? now that i think of it how can you manually destroy scruffy? I know you can remove all pointers to scruffy and let the garbage collector pick him up but there must be a manual way is there not?
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #9 of 18
Quote:
Originally Posted by Sin Cities Sin;13859452 
i know all about oop in java how do you create and destroy an object in c++? now that i think of it how can you manually destroy scruffy? I know you can remove all pointers to scruffy and let the garbage collector pick him up but there must be a manual way is there not?

java has automatic garbage collection...c++ is in the hands of the programmer.

Everytime you use the word "new" you want to have a corresponding delete.

for deallocating memory in conjunction with an object
Code:
Dog* scruffy = new Dog;
// use scruffy
delete scruffy;

deallocating memory for a dynamic array...look at where the brackets are...
Code:
int* set = new int[100];
//use set[]
delete [] set;

check out destructors and how they are used...and also when to use them...as they are very important with handling the "freeing" of memory.
Edited by surfbumb - 6/13/11 at 4:13pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #10 of 18
this is very good i plan on learning c++ when i am done with c im trying to do it all at once, Java and C i mean, In C++ just delete scruffy and he is gone. Can you not call the garbage collector on an object in java to remove it like the delete function in C++?
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  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 › Set variable equal to a method return in Java