Jump to content

Java Variables

grimFandingo

Hello everyone;

 

So I have decided to learn Java after many years out of the game (was a C programmer)

 

I already decided I hate the variables system, so I need some help... (Bear in mind this is not the actual code I am using this is just to make it easier)

 

Say I have one variable in a method in a class that is not the main class

public void method1(){int a = 1System.out.println(a + 1);}

then I want to use it inside another method in the same class

public void method2(int a){System.out.println("a + 2")

So far this is fine, but here is my issue.

 

When I want to call method2 in my main class I get the error "a cannot be resolved to a variable"

 

Here is what I am doing:

 

I am turning the method into an object and then calling it like I do with methods without parameters:

testClass method2obj = new testClass();method2obj.method2(a);

I am not sure what to do here, any ideas?

 
Thanks guys

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

public void method2(int a){   System.out.println(a + 2);}

then to call:

method2(5);

outcome:

7

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to comment
Share on other sites

Link to post
Share on other sites

testClass method2obj = new testClass();method2obj.method2(a);

 

What exactly are you trying to do here? It's stating a cannot be resolved to a variable, because in this code the variable a has not been set, and so it does not exist. If you're attempting to use the value of a inside the class, then you need to reference it, such as:

 

method2obj.method2(method2obj.a);

Though I haven't touched java in about 6 months now so I might incorrect in the referencing method used, but you need to ensure that the variable is public and accessible outside of the class. That, or you need to simply define a before calling method2() in your main function (or wherever you're calling it) as without an object-specific reference, Java does not attempt to retrieve the instance variable, and instead looks for a locally scoped variable, a.

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks guys, my problem is solved;

 

just quickly though,

 

is there a way to run a method only the first time an application is ran then never do it again. It will save the required details and remember them for next time. Is this possible without incorporating a database?

Link to comment
Share on other sites

Link to post
Share on other sites

Doesn't matter, I used an array and found the answer 

do you care explaining what fixed the issue in the first place :)

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to comment
Share on other sites

Link to post
Share on other sites

I would assume you can store the array to a file, and read the file every time the program is launched. If file exists, don't run method. If file doesn't exist, run method. Can't do it purely with arrays/variables, as everything is reset when you close a program. Files are the simplest choice for small amounts of data, databases for large amounts of data.

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×