• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A simple question on System.out.println

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the second case, when should the code be executed?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic