I don't why I get 'not a statement' error in the following code...

C

Chad

public class hw1q3 {

public static void main(String[] args) {
Test a = new Test();
a.showAllOne();
a.showAllTwo();
System.out.println("The value of x is " + Test.x); //I get x
is 2
Test.x; //I get a compile error that says - not a statement
}
}

class Test {

public static final int w = 1;
public static int x = 2;
public final int y = 3;
public int z = 4;

public void showAllOne() {
System.out.println("w is " + w);
System.out.println("x is " + x);
System.out.println("y is " + y);
System.out.println("z is " + z);
}

public static void showAllTwo() {
System.out.println("w is " + w);
System.out.println("x is " + x);
//System.out.println("y is " + y);
//System.out.println("z is " + z);
}
}


I don't get why 'Test.x;' in main() isn't a variable. The expression
'Test.x' ends with a semicolon. So I just assumed it was a valid
statement. However, the java compiler tells me something different.
Ideas? Possible hints?

Chad
 
C

Chad

On 10/4/2011 11:16 AM, Chad wrote:
...


See the JLS,http://java.sun.com/docs/books/jls/third_edition/html/statements.html...

Only certain types of expressions can be turned into a statement by
adding a semicolon. They all have at least potential for side-effects at
the top level.

May I ask why you need to execute Test.x without doing anything with the
result?

Because I was trying to figure out the error. The actual line of code
is something like

Test.x += x;

But when I stripped the line to

Test.x;

I got the same error.

Chad
 
D

Daniel Pitts

Because I was trying to figure out the error. The actual line of code
is something like

Test.x += x;

But when I stripped the line to

Test.x;

I got the same error.

Chad

"Test.x += x;" is not the same as "Test.x";

Also, "Test.x" is fine, but "x" (in class hw1q3) is not defined.
 
D

Daniel Pitts

I still don't see why this expression can't be turned into a
statement.

Chad
What does "Test.x;" mean to you? If it successfully compiled, what would
you expect that it does?

Test.x is a variable reference, you either need to read it or write it.
 
C

Chad

What does "Test.x;" mean to you? If it successfully compiled, what would
you expect that it does?

Test.x is a variable reference, you either need to read it or write it.

I thought it did the same thing as something like a.x

Chad
 
S

Stanimir Stamenkov

Tue, 4 Oct 2011 14:55:12 -0700 (PDT), /Chad/:
I still don't see why this expression can't be turned into a
statement.

Because it doesn't match any of the syntax constructions given in
the Java Language Specification referenced above (learn to read):

LocalVariableDeclarationStatement
ClassDeclaration
Statement

Your statement |Test.x;| is obviously not a local variable
declaration nor class declaration, further:

Statement:
StatementWithoutTrailingSubstatement
LabeledStatement
IfThenStatement
IfThenElseStatement
WhileStatement
ForStatement

Yours is none of labeled, if-then, if-then-else, while or for
statement, so:

StatementWithoutTrailingSubstatement:
Block
EmptyStatement
ExpressionStatement
AssertStatement
SwitchStatement
DoStatement
BreakStatement
ContinueStatement
ReturnStatement
SynchronizedStatement
ThrowStatement
TryStatement

The only posibility here is expression statement:

ExpressionStatement:
StatementExpression ;

StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression

Which of the given above should match your statement? Yours is
neither assignment nor pre-increment, nor pre-decrement, nor
post-increment, nor post-decrement, nor method invocation, nor
class instance creation.
 
C

Chad

Primarily, it is a matter of language definition, which is why I
included a link to the Java Language Specification.

Are you saying that you think it is valid according to the JLS, or are
you saying that you do not understand why the Java designers decided not
to permit it?

I guess I don't understand why the Java designers decided not to
permit it.

Chad
 
R

Roedy Green

public class hw1q3 {
Class name must start with capital letter.
See http://mindprod.com/jgloss/codingconventions.html
public static void main(String[] args) {
Test a = new Test();
a.showAllOne();
a.showAllTwo();
System.out.println("The value of x is " + Test.x); //I get x
is 2
Test.x; //I get a compile error that says - not a statement
Test.x is a variable. You have to do something with it, e.g. pass it
as a parameter, add 1 to it. .
}
}

class Test {

public static final int w = 1;
public static int x = 2;
public final int y = 3;
public int z = 4;
is real life you never expose your variables like that. You use a
getter/setter.
public void showAllOne() {
System.out.println("w is " + w);
System.out.println("x is " + x);
System.out.println("y is " + y);
System.out.println("z is " + z);
}

public static void showAllTwo() {
System.out.println("w is " + w);
System.out.println("x is " + x);
//System.out.println("y is " + y);
//System.out.println("z is " + z);
}
}
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
 
E

Eric Sosman

[... concerning non-statements like `42;' ...]

I guess I don't understand why the Java designers decided not to
permit it.

Java's designers chose to enshrine only some of C's infelicities.

Here's another one they didn't perpetuate:

String[] array = { "I", "am", "commutative" };
String s = 1[array];

"Others will occur to your thought." -- Gandalf
 
M

markspace

[... concerning non-statements like `42;' ...]

I guess I don't understand why the Java designers decided not to
permit it.

Java's designers chose to enshrine only some of C's infelicities.

Here's another one they didn't perpetuate:

String[] array = { "I", "am", "commutative" };
String s = 1[array];

"Others will occur to your thought." -- Gandalf


#define static

Don't laugh I've seen it done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top