• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

CONSTRUCTOR DECLARATION

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class MyClass{
static int staticcounter;
int instancecounter;
public static void main(String[] args){

for (int i=0;i<5; i++)<br /> new MyClass();<br /> }<br /> }<br /> public MyClass() {<br /> staticcounter++;<br /> instancecounter++;<br /> System.out.println("Static Counter="+staticcounter);<br /> System.out.println("\ninstancecounter="+instancecounter);<br /> }<br /> }<br /> Once I try to compile this program, I get the following message.Compiler wants class keywork with MyClass constructor, But I think that is not the way constructors are declared!!<br /> <br /> C:\jdk1.2.1\bin>javac MyClass.java
MyClass.java:12: 'class' or 'interface' keyword expected.
public MyClass() {
^
MyClass.java:12: Class MyClass already defined in MyClass.java.
public MyClass() {
^
MyClass.java:12: '{' expected.
public MyClass() {
^
3 errors
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If u have a careful look at the code u posted,u will find that there is an extra bracket } after the main method.
I *really* dont think that this a problem with the way constructors are declared.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Try this code...it works fine.
OUTPUT :
Static Counter= 5
Instance Counter= 5
public class Check{
static int staticcounter;
static int instancecounter;
public Check() {
staticcounter++;
instancecounter++;}
public static void main(String[] args){
for (int i=0;i<5; i++)
new Check();
System.out.println("Static Counter= "+ staticcounter);
System.out.println("\nInstance Counter= "+ instancecounter);}}
Mukti
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You have decleared the class constractor out side the class, that's why you are getting the error message at compile time.
Remove the extra '}' before the class constractor and compile your code again it will work.
Good luck
Amit
 
Men call me Jim. Women look past me to this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic