Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: use of System.out.println(""); error....

  1. #1
    Junior Member
    Join Date
    Jul 2011
    My Mood
    Thanked 0 Times in 0 Posts

    Default use of System.out.println(""); error....

    Hi all,
    Cant we use System.out.println within class scope ? Like the one shown below:

    class A{
     int a=1;
    System.out.println(" a is  " +a);     //giving error ... understandable
    System.out.println();                   // but why error for this..?
       void cal(){
           int b=2;
             System.out.println(" b is " +b);   //working fine
    public class Test {
        public static void main(String[] args) {
        A a =new A();

    Why doesn't java allow this? I mean Should i explicitly write a method to display the value of variables of a particular class? OR Do it this way which is as told in java in main()
    A a = new A();
    System.out.println("a is " +a.a);
    Last edited by rohan22; July 15th, 2011 at 05:54 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,432 Times in 2,383 Posts

    Default Re: use of System.out.println(""); error....

    The println calls need to be inside of a method or constructor.
    Statements outside of methods are executed when the class is instantiated. They are for defining and initializing variables.

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Faversham, Kent, UK
    My Mood
    Thanked 58 Times in 54 Posts

    Default Re: use of System.out.println(""); error....

    You can do this if you put your printing code inside an initialization block. This will get executed once for every instance, before the constructor runs. For example:
    class A {
        int a = 1;
        {                                            // open initialization block
            System.out.println(" a is  "+a);     
        }                                            // close initialization block
        void cal() {
            int b = 2;
            System.out.println(" b is " + b);   //working fine
    This is not recommended practice unless you have a good reason for doing it, and in my experience, is rarely, if ever necessary.

  4. The Following User Says Thank You to dlorde For This Useful Post:

    derekeverett (July 19th, 2011)

Similar Threads

  1. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  2. System.out.println("Hello World!")
    By Crixus in forum Member Introductions
    Replies: 1
    Last Post: April 24th, 2011, 10:08 AM
  3. System.out.println ("Hellow");
    By OnionzRingz in forum Member Introductions
    Replies: 1
    Last Post: March 1st, 2011, 05:56 PM
  4. System.out.println("Hi!");
    By 342-173=147 in forum Member Introductions
    Replies: 1
    Last Post: February 27th, 2011, 03:07 PM
  5. System.out.println ("Hello world!")
    By ShaunB in forum Member Introductions
    Replies: 0
    Last Post: December 29th, 2009, 05:17 PM