Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 414,938 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,691 people online right now.Registration is fast and FREE... Join Now!



non-static method cannot be referenced from a static context im a newbie programmer, plz help Rate Topic: -----

#1 yellowbus  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-September 07


Dream Kudos: 0

Share |

non-static method cannot be referenced from a static context

Posted 07 September 2007 - 06:20 PM

I have a "non-static method cannot be referenced from a static context" problem. I've research similar cases in google, but I can't make the error go away no matter what.
Sry for being nubby. barely one year of programming:(
Basically, I'm trying to call a method from another class, and make it print on screen.

public class Show{
   public static void main(String[] args) {
		int x=1;
		int a=4;
		int b=2;
		x = Combination.comb(a,b);
		System.out.println(x);
	}	}



This is the Combination class that i am trying to call. first method does factorials, second method uses factorials to do combination


public class Combination {

	
public int factorial(int x) {
int result=1;
for(int i=1;i<=x;i++){
	result=i * result;
}
return result;
}

public int comb(int n, int k)
{
int combin=1;
int temp4=1;
int temp1 = factorial(n);
int temp2 = factorial(k);
temp4 = n-k;
int temp3 = factorial (temp4);
combin = temp1/(temp2 * temp3);
return combin;
}
}



Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1434
  • View blog
  • Posts: 8,313
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: non-static method cannot be referenced from a static context

Posted 07 September 2007 - 06:35 PM

You simply have to make the functions of your class static. Since you are not creating instances of your classes, and thus wanting to use methods statically, you simply have to declare the methods static as so...

public static int factorial(int x) {

and 

public static int comb(int n, int k) {



Just drop in the word static between the modifier and the return type and recompile. You are all ready to go. :)

This post has been edited by Martyr2: 07 September 2007 - 06:36 PM

Was This Post Helpful? 1

#6 yellowbus  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-September 07


Dream Kudos: 0

Re: non-static method cannot be referenced from a static context

Posted 07 September 2007 - 06:44 PM

thank you, i've read similar solutions to this problem but i couldn't apply it to my own program (newb-ness). it works perfectly, thanks for the fast reply
Was This Post Helpful? 0
  • +
  • -

#7 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • Icon

Reputation: 14
  • View blog
  • Posts: 355
  • Joined: 24-October 06


Dream Kudos: 50

Re: non-static method cannot be referenced from a static context

Posted 11 September 2007 - 09:41 AM

The reason you need to make these static is that no classes are defined as instances (like Martyr2 said). I just want to clarify what he said:
  Combination comb1 = new Comination();
  // or
  Combination comb2;


Was This Post Helpful? 0
  • +
  • -

#8 yellowbus  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-September 07


Dream Kudos: 0

Re: non-static method cannot be referenced from a static context

Posted 11 September 2007 - 04:27 PM

I had another program with the same program problem, and i tried to switch double perimeters = perimeter(s1,s2,s3);
with
perimeters = new perimeter();
just like ajwsurfer said, but then i got even more errors (nonstatic method perimeter(double,double,double) cannot be referenced from static context). i put astericks on the problem lines. can anyone help me? i tried to apply the same solution but it wouldn't work.
public class triangle{
   public static void main(String[] args) {

double s1=0;
double s2=0;
double s3=0;

	System.out.println("Enter side 1 of the triangle:");
	s1 = TextIO.getlnInt();
	System.out.println("Enter side 2 of the triangle:");
	s2 = TextIO.getlnInt();
		System.out.println("Enter side 3 of the triangle:");
	s3 = TextIO.getlnInt();
	

	***double perimeters = perimeter(s1,s2,s3);
	***double areas  = area(s1,s2,s3);
	System.out.println("Perimeter is " + perimeters);
	System.out.println("Area is " + areas);
   }

   
   	public double perimeter(double s1, double s2, double s3){	
   		double peri;
   		peri = s1 + s2 + s3;
   		return peri;
   	}
   	
   	public double area(double s1, double s2, double s3){	
   		double area1;
   		double temp;
   		temp = (s1 + s2 + s3)/2;
   		area1 = temp * (temp-s1) * (temp-s2) * (temp-s3);
   		return area1;
   	}
   	
   	
   	
   
   }


Was This Post Helpful? 0
  • +
  • -

#9 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1434
  • View blog
  • Posts: 8,313
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: non-static method cannot be referenced from a static context

Posted 11 September 2007 - 05:38 PM

Again you need to put static on both of your functions since you are calling them from static main(). Whenever you call another local function from main (which is static) then they have to be static too. That is just the general rule.

:)
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users