• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a little question

 
peterx peter
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
This class do nothing but I want to test this code.
Why do I get compile error.
Error after compiling: test.a is not statement
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can just place a variable like on a line. You have to something with the variable.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because "test.a;" is not a statement. If you are coming from a C background, you know that in C you can turn any expression into a statement by putting a semicolon after it:

x=y; //C and Java syntax

x==y; //C, but not Java, syntax

The trouble with this simple approach to statements is that not all expressions make sensible statements, so Java limits the expression ; syntax to:

; //the empty statement, if that counts

x=y; //assignment

i++;
++i;
i--;
--i; //increment and decrement

x.m(); //method invocation

new X(); //object creation

What you wrote, "test.a;", was just accessing a field, and so not a legitimate statement. Try assigning it or printing it:

int x = test.a;
//or
System.out.println(test.a);
 
peterx peter
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff Albertson, Keith Lynn!
Thank you very much for your help.
peter
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, first off to do test.a, you would have to declare a method 'a'.

Like so:

class test
{
public void a()
{
int integer = 10;
return integer;
}
}

something like that.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*public int a(){}*

sorry, if it returns a int value you have to declare the method of return type int.
 
R Kharat
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi buddy,
Just try putting there a S.O.P statement.
i.e a System.out.println(test.a);
This should display your value.

Thanks!
Rohit.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Monk Fox:
ok, first off to do test.a, you would have to declare a method 'a'.



This is incorrect. "test.a" does NOT refer to a method. It refers to a member variable, which is exactly what the original poster has.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic