This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A simple question on System.out.println

 
Madhu Kumar Vatts
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Siripa Siangklom
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the second case, when should the code be executed?
 
Suhaasi Karnik
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic