| Author |
A simple question on System.out.println
|
Madhu Kumar Vatts
Ranch Hand
Joined: Apr 01, 2004
Posts: 67
|
|
I have two pieces of code 1. public class Stringmethods { public void testmethod() { String s="airplane"; System.out.println("Number of exceptions thrown rows:"); } } 2. public class Stringmethods { public void testmethod() { String s="airplane"; } System.out.println("Number of exceptions thrown rows:"); } the second one (2)doesnt work. Can any body answer why it is working when we keep it in a method and why not when it is in a class
|
Oracle Certified Enterprise Architect (Part 1)
SUN Certified Programmer,
SUN Certified Business Component Developer
SUN Certified Web Component Developer
Oracle Certified WebServices Developer
Oracle Certified Java Persistence API Developer
Oracle Certified SQL Expert
IBM Certified XML Solution Developer
|
 |
Siripa Siangklom
Ranch Hand
Joined: Jan 26, 2004
Posts: 79
|
|
the second one (2)doesnt work. Can any body answer why it is working when we keep it in a method and why not when it is in a class
In OO concepts, software objects interact and communicate with each other using messages.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
The short answer is that you have to put Java statements inside methods -- that's just the rule. The slightly longer answer is that a variable declaration with or without an initializer isn't a statement, it's a declaration, and those can appear at class scope; but all other code must be inside a method or block.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
In the second case, when should the code be executed?
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Suhaasi Karnik
Greenhorn
Joined: Mar 15, 2004
Posts: 23
|
|
You could think of it in this way: A class describes the structure of the object and its functionality. Structure is described by member variables. eg. String item="airplane"; Functionality is described as methods. e.g. System.out.println("Zoooooom awaaaaaaay"); Hence System.out, which describes the behaviour of the object, goes in a method. [ October 05, 2004: Message edited by: suhaasi karnik ]
|
 |
Mahesh Bhatt
Ranch Hand
Joined: Sep 15, 2004
Posts: 88
|
|
Hi all, That's true, in java, Other than the variable declarations, all the code has to be within the body of methods. because in java the communication happens only through methods of a class. If some code is outside a method then that gives a compilation error. [ October 05, 2004: Message edited by: prashant bhogvan ]
|
Impossible is I M Possible
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by prashant bhogvan: Hi all, That's true, in java, Other than the variable declarations, all the code has to be within the body of methods.
That's not fully true: constructors aren't nethods; and you can have static and instance initializers.
|
 |
 |
|
|
subject: A simple question on System.out.println
|
|
|