Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in System.out.println

 
vinaykumar singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all...

i having a problem in System.out.println();
see code and please guide me.(i m using Eclipse 3.0,and also tried this code in diffrent editor)



public class A {

System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}

i have a another main class where i calling class A.

get error in class A
System.out.println();

Multipal markers at this line
-Syntax error on token ""aaaaaa"", delete this token
-Syntax error on Token(s), misplaced construct(s)
-----------------------------------------------------------

if i write only

public class A {

System.out.println();
}

getting
-Syntax error on token "println", Identifier expected after this token

-------------------------------------------------------------------------
and if i decleared main method in class A there is no error
like

public class A {
public static void main(String args[]){
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}
}

-No Error

-------------------------------------------------------------------------

please guide me why this is happening.

thanks in advance...

vinay kumar singh
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first example, you can't have a statement that's not in a method. you could put the method in a constructor if you want it to print when the object is created, or you have to define a method like


and then call that method on the object.

in the second example, i think it's because there is no version of println that takes no argument - you have to pass it SOMETHING. and, if you change it to pass in a parameter, you'll be back at the first example.

in your third example, you have an enclosing method and a valid call to the println method.
[ January 17, 2006: Message edited by: fred rosenberger ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:
in the second example, i think it's because there is no version of println that takes no argument - you have to pass it SOMETHING.

Nope, the second example has the same problem as the first. You are only allowed to put variable declarations outside of a method. Any code that is to be executed must be inside a method, as Fred says. There is a version of System.out.println() that takes no arguments.

Layne
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was just taking a stab, since we got a different error than the first one, and i didn't have time to doublecheck the API.

at least i was smart enough to write the "i think..."

in any case, i hope we've answered the original question.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic