Calling an int from another class

cant make a static reference to a non-static method.. what to do!?

Page 1 of 1

3 Replies - 2781 Views - Last Post: 24 June 2009 - 01:13 PM Rate Topic: -----

#1 Shedlock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-June 09

Calling an int from another class

Posted 24 June 2009 - 11:43 AM

Hi guys,

this is my first help post! i'm very new to java.

Im trying to call an int from the class InputVal into the class Converter..
I'm getting an error that says Cannot make a static reference to the non-static method getJopinput() from the type InputVal

below is my code.. i have no idea whats wrong!

InputVal class
 package Shedlock1;

import javax.swing.JOptionPane;

public class InputVal 
{
	private String binary;
	private boolean isValid;
	public int jopinput;

	public InputVal() 
	{
		isValid = false;
	} // end constructor

	public void input() 
	{
		do 
		{
			binary = JOptionPane.showInputDialog("Enter an 8 digit number \n"
					+ "beginning with a one.");
			if ((binary.length() == 8) && (binary.charAt(0) == '1')) 
			{
				for (int i = 0; i < binary.length(); i++) 
				{
					char c = binary.charAt(i);
					if ((c == '0') || (c == '1')) 
					{
						isValid = true;
					} 
					else 
					{
						isValid = false;
						break;
					}// end else
				}// end for i
			} 
			else 
			{
				isValid = false;
			}// end else
		} while (!isValid);
		int jopinput = Integer.valueOf(binary);//converts binary to an int		
	} // end input
	public String getBinary() 
	{
		return binary;
	}
	public int getJopinput () 
	{ 
		return jopinput; 
	}//makes us able to call the int in converter
} // end class InputVal



Converter class
package Shedlock1;

import java.text.NumberFormat;
import javax.swing.JOptionPane;

public class Converter {
	public String temp;
	public String season;
	final double SENTINEL = 109;
	
	public Converter() {
		{
			InputVal.getJopinput();
			
			int converted = 10001001; // converts input from JOptionPane from
										// binary to a number in the decimal
										// system
			while (converted > SENTINEL) {
				if (converted >= SENTINEL) {
					converted = (converted / 4);
				} // end if
			} // end while (Temperatures)
			JOptionPane.showMessageDialog(null, "The temperature is "
					+ converted + "F");

		}
	}
}


any help is greatly appreciated.. sorry for the long codes!

Is This A Good Question/Topic? 0
  • +

Replies To: Calling an int from another class

#2 kmangold  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: Calling an int from another class

Posted 24 June 2009 - 11:46 AM

You need to either make that field or method a static method, or call the non-static method from an instance of that class.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Calling an int from another class

Posted 24 June 2009 - 11:50 AM

 
 InputVal.getJopinput();



getJopinput() is a method that belongs to an instance of InputVal
you cannot just call it like that

If it like if you have a class Person with a method getAge
you cannot do Person.getAge() to which person do you reger to ?

You have to

Person fred = new Person(40);
Person john = new Preson(25);

now you get do
fred.getAge(); or john.getAge();

in your case you have to

InputVal x = new InputVal();
x.getJoinput();
Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,175
  • Joined: 27-December 08

Re: Calling an int from another class

Posted 24 June 2009 - 01:13 PM

Let's talk about static vs. non-static calls.

Static call: Calling a member or method directly from a class, not an instantiated object. Ex:

JFrame.EXIT_ON_CLOSE




Non-Static Call: Calling a member or method from an object/variable, not from the originating class. Ex:

ArrayList<Object> o = new ArrayList<Object>();
o.get(0);



Notice in one you reference the class while in the other you reference a variable instantiated from a class. Hope this helps!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1