Hi, sorry my title isn't very explanatory. What I need to do is to access a variable from a main function, in another function without using a parameter to do so. Based on the nature of the problem you might see why I would do this.
(if there is a way using parameters, that would work too.)
I want the variable String stringFunction to be accessible by the function paintComponent. The get/set methods I have here work in the confines of the main method but not outside of it.
If what I need is unclear, please ask for clarification.

*parts have been omitted that are unrealated to my problem, denoted by .........

public class PlotExpression extends JFrame {
	private String stringFunction;
	public void setStringFunction(String x){
		stringFunction = x;
	public String getStringFunction(){
		return stringFunction;
	public PlotExpression(){
		add(new ExpressionPanel());
	public static void main(String[] args) {
		if(args.length != 3){
			System.out.println("Usage:  java PlotExpression [Function] [X bound] [Y bound]");
		String stringFunction = args[0];	
		String xBoundString = args[1];
		String yBoundString = args[2];
		System.out.println("Function:  "+args[0]+"\nX Bound:  "+args[1]+"\nY Bound:  "+args[2]);		
		PlotExpression frame = new PlotExpression();
	protected void paintComponent(Graphics g){
		int xorigin = 50;
		int yorigin = 950;
		//String stringFunction = s;
		int x[] = {xorigin,xorigin,yorigin};
		int y[] = {xorigin,yorigin,yorigin};		
		//attempt to graph y = x*2
		int bound = 9;
		int xpoly[] = new int[bound];			//declare points to be made
		for(int i = 0; i<bound; i++){			//read in command line function here
			xpoly[i] = i*xorigin + xorigin;	
		int ypoly[] = new int[bound];			//declare points to be made
			for(int i = 0; i<bound; i++){
			ypoly[i] = yorigin - ((xpoly[i]*2)-2*xorigin);	//read in command line function here 


My first issue in using parameters exists here:

public PlotExpression(){
	add(new ExpressionPanel());

and here

PlotExpression frame = new PlotExpression();

Because of this, I am unsure how to include a new parameter for a string. If parameters aren't the way to go, I haven't been able to let paintComponent have access to stringFunction either.

Edited by ckwolfe: n/a

5 Years
Discussion Span
Last Post by JamesCherrill

Not sure I understand the whole of your requirement, but
If PlotExpression needs a value for stringFunction in order to work, and especially if that value is unlikely to change within one instance of PlotExpression, then passing it as a parameter to the constructor would be the obvious way to go.
Eg if my Person class must have the person's name:;

class Person {
   private String name;
   public Person(String personsName) {
      name = personsName;
   paintComponent(Graphics g){
      g.drawString(name, ...

Okay i've had a chance to sleep on it and I think I can clarify what I'm looking for. Refer to my first post for clarification:
I use the main function in PlotExpression to read in strings from the command line at runtime (this is a requirement for the assignment), and I need to get that variable, as named by stringFunction in the main, to my paintComponent function. Using the get/set functions, I have not been able to do this.


I just came back to post that this solution worked perfectly, Thank you so much. I've been out of practice in Java so these little things trip me up some times. Thanks again!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.