Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #8538 > unrolled thread
| Started by | Chad <cdalten@gmail.com> |
|---|---|
| First post | 2011-10-04 11:16 -0700 |
| Last post | 2011-10-05 12:00 -0700 |
| Articles | 17 — 8 participants |
Back to article view | Back to comp.lang.java.programmer
I don't why I get 'not a statement' error in the following code... Chad <cdalten@gmail.com> - 2011-10-04 11:16 -0700
Re: I don't why I get 'not a statement' error in the following code... Patricia Shanahan <pats@acm.org> - 2011-10-04 11:33 -0700
Re: I don't why I get 'not a statement' error in the following code... Chad <cdalten@gmail.com> - 2011-10-04 11:43 -0700
Re: I don't why I get 'not a statement' error in the following code... markspace <-@.> - 2011-10-04 12:14 -0700
Re: I don't why I get 'not a statement' error in the following code... Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-04 12:20 -0700
Re: I don't why I get 'not a statement' error in the following code... Chad <cdalten@gmail.com> - 2011-10-04 14:55 -0700
Re: I don't why I get 'not a statement' error in the following code... Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-04 15:09 -0700
Re: I don't why I get 'not a statement' error in the following code... Chad <cdalten@gmail.com> - 2011-10-04 15:11 -0700
Re: I don't why I get 'not a statement' error in the following code... Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-04 17:30 -0700
Re: I don't why I get 'not a statement' error in the following code... Stanimir Stamenkov <s7an10@netscape.net> - 2011-10-05 01:19 +0300
Re: I don't why I get 'not a statement' error in the following code... Patricia Shanahan <pats@acm.org> - 2011-10-04 22:16 -0700
Re: I don't why I get 'not a statement' error in the following code... Chad <cdalten@gmail.com> - 2011-10-05 08:13 -0700
Re: I don't why I get 'not a statement' error in the following code... Patricia Shanahan <pats@acm.org> - 2011-10-05 10:21 -0700
Re: I don't why I get 'not a statement' error in the following code... Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-05 20:43 -0400
Re: I don't why I get 'not a statement' error in the following code... markspace <-@.> - 2011-10-05 19:36 -0700
Re: I don't why I get 'not a statement' error in the following code... Lew <lewbloch@gmail.com> - 2011-10-05 19:43 -0700
Re: I don't why I get 'not a statement' error in the following code... Roedy Green <see_website@mindprod.com.invalid> - 2011-10-05 12:00 -0700
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2011-10-04 11:16 -0700 |
| Subject | I don't why I get 'not a statement' error in the following code... |
| Message-ID | <de7d2141-f562-452d-b754-46cab58ef211@d18g2000yql.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2011-10-04 11:33 -0700 |
| Message-ID | <ut2dnZcFmYX1zBbTnZ2dnUVZ_q2dnZ2d@earthlink.com> |
| In reply to | #8538 |
On 10/4/2011 11:16 AM, Chad wrote: ... > 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? See the JLS, http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.8 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? Patricia
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2011-10-04 11:43 -0700 |
| Message-ID | <0441a828-514e-45b9-8481-ddb105e25d5f@x19g2000vbl.googlegroups.com> |
| In reply to | #8539 |
On Oct 4, 11:33 am, Patricia Shanahan <p...@acm.org> wrote: > On 10/4/2011 11:16 AM, Chad wrote: > ... > > > 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? > > 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
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-10-04 12:14 -0700 |
| Message-ID | <j6fluh$ma0$1@dont-email.me> |
| In reply to | #8540 |
On 10/4/2011 11:43 AM, Chad wrote: > Because I was trying to figure out the error. The actual line of code > is something like > > Test.x += x; What variable does the second "x" refer to here?
[toc] | [prev] | [next] | [standalone]
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Date | 2011-10-04 12:20 -0700 |
| Message-ID | <P%Iiq.1732$Gu.1264@newsfe14.iad> |
| In reply to | #8540 |
On 10/4/11 11:43 AM, Chad wrote: > On Oct 4, 11:33 am, Patricia Shanahan<p...@acm.org> wrote: >> On 10/4/2011 11:16 AM, Chad wrote: >> ... >> >>> 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? >> >> 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 "Test.x += x;" is not the same as "Test.x"; Also, "Test.x" is fine, but "x" (in class hw1q3) is not defined.
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2011-10-04 14:55 -0700 |
| Message-ID | <7de755e8-3339-4c3c-95a0-03d0e4aab701@5g2000yqo.googlegroups.com> |
| In reply to | #8539 |
On Oct 4, 11:33 am, Patricia Shanahan <p...@acm.org> wrote: > On 10/4/2011 11:16 AM, Chad wrote: > ... > > > 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? > > 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? > I still don't see why this expression can't be turned into a statement. Chad
[toc] | [prev] | [next] | [standalone]
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Date | 2011-10-04 15:09 -0700 |
| Message-ID | <3uLiq.5280$yO1.4957@newsfe21.iad> |
| In reply to | #8548 |
On 10/4/11 2:55 PM, Chad wrote: > On Oct 4, 11:33 am, Patricia Shanahan<p...@acm.org> wrote: >> On 10/4/2011 11:16 AM, Chad wrote: >> ... >> >>> 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? >> >> 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? >> > > 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.
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2011-10-04 15:11 -0700 |
| Message-ID | <9a31535b-20bd-4213-ab38-8e7d1bd1a7a3@dd6g2000vbb.googlegroups.com> |
| In reply to | #8549 |
On Oct 4, 3:09 pm, Daniel Pitts <newsgroup.nos...@virtualinfinity.net> wrote: > On 10/4/11 2:55 PM, Chad wrote: > > > > > On Oct 4, 11:33 am, Patricia Shanahan<p...@acm.org> wrote: > >> On 10/4/2011 11:16 AM, Chad wrote: > >> ... > > >>> 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? > > >> 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? > > > 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. I thought it did the same thing as something like a.x Chad
[toc] | [prev] | [next] | [standalone]
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Date | 2011-10-04 17:30 -0700 |
| Message-ID | <SyNiq.2724$pH6.86@newsfe10.iad> |
| In reply to | #8550 |
On 10/4/11 3:11 PM, Chad wrote: > On Oct 4, 3:09 pm, Daniel Pitts<newsgroup.nos...@virtualinfinity.net> > wrote: >> On 10/4/11 2:55 PM, Chad wrote: >> >> >> >>> On Oct 4, 11:33 am, Patricia Shanahan<p...@acm.org> wrote: >>>> On 10/4/2011 11:16 AM, Chad wrote: >>>> ... >> >>>>> 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? >> >>>> 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? >> >>> 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. > > I thought it did the same thing as something like a.x > > Chad Okay, what does "a.x" mean to you? Same question. a.x; will not compile.
[toc] | [prev] | [next] | [standalone]
| From | Stanimir Stamenkov <s7an10@netscape.net> |
|---|---|
| Date | 2011-10-05 01:19 +0300 |
| Message-ID | <j6g0p4$6vm$1@dont-email.me> |
| In reply to | #8548 |
Tue, 4 Oct 2011 14:55:12 -0700 (PDT), /Chad/:
> On Oct 4, 11:33 am, Patricia Shanahan 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?
>
> 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.
--
Stanimir
[toc] | [prev] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2011-10-04 22:16 -0700 |
| Message-ID | <peCdnd3vqM_YdRbTnZ2dnUVZ_tmdnZ2d@earthlink.com> |
| In reply to | #8548 |
On 10/4/2011 2:55 PM, Chad wrote: > On Oct 4, 11:33 am, Patricia Shanahan<p...@acm.org> wrote: >> On 10/4/2011 11:16 AM, Chad wrote: >> ... >> >>> 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? >> >> 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? >> > > I still don't see why this expression can't be turned into a > statement. 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? Patricia
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2011-10-05 08:13 -0700 |
| Message-ID | <bdc2e347-7d08-4b52-bfc2-cc17de9fd330@20g2000yqq.googlegroups.com> |
| In reply to | #8553 |
On Oct 4, 10:16 pm, Patricia Shanahan <p...@acm.org> wrote: > On 10/4/2011 2:55 PM, Chad wrote: > > > > > > > On Oct 4, 11:33 am, Patricia Shanahan<p...@acm.org> wrote: > >> On 10/4/2011 11:16 AM, Chad wrote: > >> ... > > >>> 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? > > >> 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? > > > I still don't see why this expression can't be turned into a > > statement. > > 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
[toc] | [prev] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2011-10-05 10:21 -0700 |
| Message-ID | <NNWdnfnEyse-DxHTnZ2dnUVZ_oadnZ2d@earthlink.com> |
| In reply to | #8574 |
On 10/5/2011 8:13 AM, Chad wrote: > On Oct 4, 10:16 pm, Patricia Shanahan<p...@acm.org> wrote: >> On 10/4/2011 2:55 PM, Chad wrote: >> >> >> >> >> >>> On Oct 4, 11:33 am, Patricia Shanahan<p...@acm.org> wrote: >>>> On 10/4/2011 11:16 AM, Chad wrote: >>>> ... >> >>>>> 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? >> >>>> 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? >> >>> I still don't see why this expression can't be turned into a >>> statement. >> >> 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. I think to reduce source code that is pure decoration, with no effect on the program as run. A Java implementation is allowed to optimize, as long as it keeps both any results that are used and any side effects. Evaluating Test.x has no side effects, and the result is dead, so the entire statement would be dead code which the implementation can, and ideally should, firmly ignore. Patricia
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-10-05 20:43 -0400 |
| Message-ID | <j6itj9$oif$1@dont-email.me> |
| In reply to | #8574 |
On 10/5/2011 11:13 AM, Chad wrote:
> [... 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
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-10-05 19:36 -0700 |
| Message-ID | <j6j47g$ouv$1@dont-email.me> |
| In reply to | #8586 |
On 10/5/2011 5:43 PM, Eric Sosman wrote:
> On 10/5/2011 11:13 AM, Chad wrote:
>> [... 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.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-10-05 19:43 -0700 |
| Message-ID | <17918847.1803.1317868995757.JavaMail.geo-discussion-forums@prib32> |
| In reply to | #8588 |
markspace wrote: > #define static > > Don't laugh I've seen it done. That shit just makes me mad. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-10-05 12:00 -0700 |
| Message-ID | <h5ap87d7vaofugsrvipf7ms0gsckfdm8fg@4ax.com> |
| In reply to | #8538 |
On Tue, 4 Oct 2011 11:16:22 -0700 (PDT), Chad <cdalten@gmail.com>
wrote, quoted or indirectly quoted someone who said :
>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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web