• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Why doesn't java allow this?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have teh following java code, Why doesn't java allow any printing after decalring the variables.. See following..
import java.*;
public class bath
{
//initialization at the point of definition
private String s1=new String("Happy"),
s2="happy",s3,s4;
soap ivory;
int i;
float toy;
//Doesn't allow to compile says type expected....
System.out.println("Inside class bath");
//
bath()
{
System.out.println("Insde Bath()");
s3=new String("joy");
i=47;
toy=3.14f;
ivory=new soap();
}
void print()
{
//Delayed intialization
if(s4==null)
s4="JUY";
System.out.println("The s1 value is"+s1);
System.out.println("The s2 value is"+s2);
System.out.println("The s3 value is"+s3);
System.out.println("The s4 value is"+s4);
System.out.println("The ivalues is"+i);
System.out.println("the toy value is"+toy);
System.out.println("the soap value is"+ivory);
}
public static void main(String args[])
{
bath b=new bath();
b.print();
}

}
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to write a block of code that is not in any method or static initializer. Can't do this. Put your print statement in a constructor or in a method and you will be fine. You are trying to do procedural programming where you just type in your code and it executes in order, but since Java is pure OOP, you have to do everything through classes and method calls.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some rules which a computer language imposes and you have to program according to those rules. A java class constitutes of data as variables and messages as methods. A class can only do things when you send a message to it, by executing its methods, so you have put your Print statements in methods or static intializers.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mehta,
This is your code with minor changes here and there. Enjoy Bath with your favourite soap IVORY. Take a good Bath.

public class Bath
{
//initialization at the point of definition
private String s1=new String("Happy"),
s2="happy",s3,s4;
String soap = "ivory";
int i;
float toy;
//Doesn't allow to compile says type expected....
//System.out.println("Inside class bath");
//
void bath()
{
System.out.println("Insde Bath()");
s3=new String("joy");
i=47;
toy=3.14f;
//ivory=new soap();
}
void print()
{
//Delayed intialization
if(s4==null)
s4="JUY";
System.out.println("The s1 value is "+s1);
System.out.println("The s2 value is "+s2);
System.out.println("The s3 value is "+s3);
System.out.println("The s4 value is "+s4);
System.out.println("The ivalues is "+i);
System.out.println("the toy value is "+toy);
System.out.println("the soap value is"+ soap);
}
public static void main(String args[])
{
Bath b=new Bath();
b.print();
}

}


 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic