This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Java 6 Compiler Error with "System.out.println("x " + x);"

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am studying for the SCJP 6 exam and have been programming all the exercises in the SCJP 6
Study Guide (Sierra, Bates). I was not sure exactly how the sample code was executing so I added the debugging tool "System.out.println(" "); at various points in the program. This caused a compiler error and I can't figure it out. I would appreciate some help on why I am getting these errors.
Here's the simple program and the errors occur with "System.out.println("x = " + x); in line 10 of the TestClass.java file.

TestClass.java

package ch3.ex;

class Bar {
int barNum = 28;
}

public class TestClass {
Bar myBar = new Bar();
int x = myBar.barNum;
System.out.println("x = " + x); // the errors occur here;

public static void main(String [] args) {
TestClass test = new TestClass();
System.out.println("test.myBar.barNum = " + test.myBar.barNum);
}

}
Result: I have no idea why I am getting this compiler error.

D:\DATA\scjp>javac -d bin -sourcepath src src\ch3\ex\*.java
src\ch3\ex\TestClass.java:10: <identifier> expected
System.out.println("x = " + x);
^
src\ch3\ex\TestClass.java:10: illegal start of type
System.out.println("x = " + x);
^
src\ch3\ex\TestClass.java:10: ')' expected
System.out.println("x = " + x);
^
src\ch3\ex\TestClass.java:10: ';' expected
System.out.println("x = " + x);
^
src\ch3\ex\TestClass.java:10: illegal start of type
System.out.println("x = " + x);
^
src\ch3\ex\TestClass.java:10: <identifier> expected
System.out.println("x = " + x);
^
src\ch3\ex\TestClass.java:10: ';' expected
System.out.println("x = " + x);
^
7 errors

Thanks in advance for your help.
Leon
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java requires code (that is not a declaration) to be in a constructor, initializer, or method. You can't just have code anywhere.

Henry
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
system.out.println is neither a variable nor a method definition. You are just calling an existing method. It is a statement. Not a definition. So you can put it in the body of a method but not outside it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic