Set variable equal to a method return in Java - Overclock.net - An Overclocking Community
Forum Jump: 

Set variable equal to a method return in Java

Reply
 
Thread Tools
post #1 of 18 (permalink) Old 06-09-2011, 01:06 PM - Thread Starter
New to Overclock.net
 
E_man's Avatar
 
Join Date: Nov 2009
Posts: 7,204
Rep: 386 (Unique: 304)
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.

Google Chrome Club

Laptop: Asus Transformer w/ Dock

Upgrades to come:, Water cooling 0/7, New IPS setup 3/3, Better Sound Setup 7/8, better PSU 1/1


E_man is offline  
Sponsored Links
Advertisement
 
post #2 of 18 (permalink) Old 06-09-2011, 01:25 PM
New to Overclock.net
 
Join Date: Dec 2009
Location: In a Box
Posts: 1,046
Rep: 30 (Unique: 28)
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.

Jtvd78 is offline  
post #3 of 18 (permalink) Old 06-09-2011, 01:36 PM
New to Overclock.net
 
CovertCover's Avatar
 
Join Date: Jun 2010
Location: Chicago, IL
Posts: 1,503
Rep: 89 (Unique: 69)
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();


CovertCover is offline  
Sponsored Links
Advertisement
 
post #4 of 18 (permalink) Old 06-09-2011, 02:06 PM - Thread Starter
New to Overclock.net
 
E_man's Avatar
 
Join Date: Nov 2009
Posts: 7,204
Rep: 386 (Unique: 304)
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.

Google Chrome Club

Laptop: Asus Transformer w/ Dock

Upgrades to come:, Water cooling 0/7, New IPS setup 3/3, Better Sound Setup 7/8, better PSU 1/1


E_man is offline  
post #5 of 18 (permalink) Old 06-11-2011, 08:31 PM
New to Overclock.net
 
Join Date: May 2011
Location: Metaverse
Posts: 1,850
Rep: 215 (Unique: 151)
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


ALUCARDVPR is offline  
post #6 of 18 (permalink) Old 06-13-2011, 11:16 AM
New to Overclock.net
 
Sin Cities Sin's Avatar
 
Join Date: Aug 2010
Location: canada
Posts: 213
Rep: 1 (Unique: 1)
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.

Sin Cities Sin is offline  
post #7 of 18 (permalink) Old 06-13-2011, 03:23 PM
New to Overclock.net
 
surfbumb's Avatar
 
Join Date: Jul 2007
Location: San Diego
Posts: 1,231
Rep: 97 (Unique: 79)
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.

surfbumb is offline  
post #8 of 18 (permalink) Old 06-13-2011, 03:29 PM
New to Overclock.net
 
Sin Cities Sin's Avatar
 
Join Date: Aug 2010
Location: canada
Posts: 213
Rep: 1 (Unique: 1)
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?

Sin Cities Sin is offline  
post #9 of 18 (permalink) Old 06-13-2011, 04:07 PM
New to Overclock.net
 
surfbumb's Avatar
 
Join Date: Jul 2007
Location: San Diego
Posts: 1,231
Rep: 97 (Unique: 79)
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.

surfbumb is offline  
post #10 of 18 (permalink) Old 06-13-2011, 04:38 PM
New to Overclock.net
 
Sin Cities Sin's Avatar
 
Join Date: Aug 2010
Location: canada
Posts: 213
Rep: 1 (Unique: 1)
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++?

Sin Cities Sin is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off