E:%26gt;javac arcm.java
arcm.java:9: non-static method fun(x) cannot be referenced from a static context
fun(c);
^
1 error
E:%26gt;
Program is given below
class arcm{
public static void main(String args[])
{
int i,j;
x c= new x();
for(i=0;i%26lt;3;i++)
for(j=0;j%26lt;4;j++)
System.out.println("s="+c.S[i][j]);
fun(c);
}
void fun(x c)
{
int i,j;
for(i=0;i%26lt;3;i++)
for(j=0;j%26lt;4;j++)
System.out.println("s_fun="+c.S[i][j]);
}
}
class x{
long S[][]={
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
}
What this java Error mean?non-static method cannot be referenced from a static context?
man o man........
its simple....make fun static and the error wuld disappear...
reason: static funcs can only call static funcs within the same class..here main is static whereas fun is nonstatic....
sometimes we do make small mistakes!
good luck
Reply:public class arcm
{
public static void main(String args[])
{
int i,j;
x c= new x();
for(i=0; i%26lt;3; i++)
{
for(j=0; j%26lt;4; j++)
{
System.out.println("s= " + c.S[i][j]);
}
}
fun(c);
}
static void fun(x c)
{
int i,j;
for(i=0;i%26lt;3;i++)
{
for(j=0;j%26lt;4;j++)
{
System.out.println("s_fun="+c.S[i][j]);
}
}
}
public static class x
{
public static long S[][];
public x()
{
S = new long[][]{
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}};
}
}
}
Reply:define fun as static void fun(x c). You're calling fun from a static function (main). fun is not part of a class definition.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment