Jump to content

How to get the value of variables from private methods in java?

Shammikit
Go to solution Solved by Nineshadow,
9 minutes ago, Shammikit said:

This didnt work,still the value of x did not pass to the public method

You use x as a global variable.

im using Netbeans as my IDE and i have a jbutton that i have created.inside its method there is a variable named x with the value 20 assigned to it.i need to access this variable from another public method.i cant change the method of the jbutton to public because my IDE doesnt allow me to do it. This is the code that i have tried.i have used the method setupgrade to send the value to another class. i printed the value of y after entering this code and the value of it prints as 0. however if i manually initialize the value of y without getting it from that private method of the jbutton,the value that i initialised will be printed.the issue as far as i can see is getting the value of x out of that private method.please help me fix this.Thank you

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {  
 int x= 20;
y=x;
}

int y;
    public void setupgrade(int d)
    {
     y=d;
    }
    public int getupgrade()
    {
    return y;
    }

 

Link to comment
Share on other sites

Link to post
Share on other sites

Go from something like:

private void foo()
{
	int x=20;
}

To something like:

int x;
private void foo()
{
	x=20;
}

?

Also, please use code tags.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, Nineshadow said:

Go from something like:


private void foo()
{
	int x=20;
}

To something like:


int x;
private void foo()
{
	x=20;
}

?

Also, please use code tags.

This didnt work,still the value of x did not pass to the public method

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Shammikit said:

This didnt work,still the value of x did not pass to the public method

You use x as a global variable.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Nineshadow said:

You use x as a global variable.

i didnt know what a global variable is so i googled it and it says : there is no concept of global variables in Java, there are only global classes with public fields. The keyword static is used to give global picture to variables. below code is an example from  a website. but when i try to initialize it as public static int x; i get a error.

public class Example
{
public static int a;
public static int b;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, Shammikit said:

i didnt know what a global variable is so i googled it and it says : there is no concept of global variables in Java, there are only global classes with public fields. The keyword static is used to give global picture to variables. below code is an example from  a website. but when i try to initialize it as public static int x; i get a error.


public class Example
{
public static int a;
public static int b;
}

 

Ah, whatever, Java's not my thing.

Just use it as a public field.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Why are you making it static? You shouldn't have to have it as a static variable to be able to access it.

 

class Example {
	private int x = 20;
	private int y;

	private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {  
 		y = x;
	}

	public void setupgrade(int d) {
		y = d;
	}

	public int getupgrade() {
		return y;
	}
}

 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Blade of Grass said:

Why are you making it static? You shouldn't have to have it as a static variable to be able to access it.

 


class Example {
	private int x = 20;
	private int y;

	private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {  
 		y = x;
	}

	public void setupgrade(int d) {
		y = d;
	}

	public int getupgrade() {
		return y;
	}
}

 

if i set the variable y to private im unable to access it from another class. if is set y to  to public and x to private it still was not able to pass the value of the variable in the private method to the variable y.in my case the variable x has a value of 20 and that value is initialized inside the jButton6ActionPerformed,like this :

 

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {  
 int x= 20;
y=x;
}
Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Nineshadow said:

You use x as a global variable.

yesterday i have typed it incorrectly. after making x a static global variable it worked.Thank you very much

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Shammikit said:

yesterday i have typed it incorrectly. after making x a static global variable it worked.Thank you very much

There it is! Oh the pain. This is how applications get made that are filled with statics and singletons...

 

This is how abortions are born.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Shammikit said:

if i set the variable y to private im unable to access it from another class. if is set y to  to public and x to private it still was not able to pass the value of the variable in the private method to the variable y.in my case the variable x has a value of 20 and that value is initialized inside the jButton6ActionPerformed,like this :

 


private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {  
 int x= 20;
y=x;
}

If you need it accessible in another class just makes a public getter. 

I'm still quite confused on what you're doing with your code. Why create a locally scoped variable for X if it's never reused? Why not just directly assign 20 to y? There's no reason why the value of y shouldn't be overwritten, perhaps if you shared the entire class it would help. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Blade of Grass said:

I'm still quite confused on what you're doing with your code.

Judging by the content of his other threads I'd surmise that he himself doesn't even know what he is trying to do... Nor care for that matter.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Nuluvius said:

Judging by the content of his other threads I'd surmise that he himself doesn't even know what he is trying to do... Nor care for that matter.

im new at java and im having a bit of trouble identifying what process needs to be done to accomplish the task i have.sorry if i confused all of you.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Shammikit said:

im new at java and im having a bit of trouble identifying what process needs to be done to accomplish the task i have.sorry if i confused all of you.

It's not really about that. There's a right and a wrong way to accomplish something. Making something static when there's no need or reason to do so is an example of a wrong way, it's also quite a dangerous thing to do in general. You were given a right way but it seems as though you have chosen to ignore it. This is indicative of not really caring or simply being lazy. Which is it?

The single biggest problem in communication is the illusion that it has taken place.

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

×