• 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

i need help

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class MyClass{
long var;
public void MyClass(long param) {var=param;}//1

public static void main(String args[]){
MyClass a,b;
a = new MyClass(); // 2
b= new MyClass(5); // 3
}
}
Here i am getting compilation error at 3, why not getting complitation error at 2 as this program dont have default constructor, can anybody help me?
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srikanth Rao:
public class MyClass{
long var;
public void MyClass(long param) {var=param;}//1

public static void main(String args[]){
MyClass a,b;
a = new MyClass(); // 2
b= new MyClass(5); // 3
}
}
Here i am getting compilation error at 3, why not getting complitation error at 2 as this program dont have default constructor, can anybody help me?


Because in the code, you have not declared a cobnstructor at all.
public void MyClass(long param) {var=param;}//1
Constructor never returns a value like void here
Hope this helps
Sri
 
Sri Rao
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnaks Sri for your help
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Making a change in declaration of the constructor will make the program run.
public class MyClass{
long var;
MyClass(long param)// change is made here.
{
var = param;
System.out.print(var);
}

public static void main(String args[])
{

MyClass n ;
n = new MyClass(5);
}
}
Pallavi
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
we need to make a point that,the difference between a method and a constructor is that method has a return type.if it does not return anything it is preceeded by "void."
But a constructor on the other hand does not have any return type not even void.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sri and SriSri -
Are you guys related ?
It reminds me of an old puzzle - Can you construct a valid and meaningful, english sentence that contains the word 'had' eleven times in a row ?
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi SriSri,
Something that the previous responders didn't underline is that your program DO HAVE a default constructor. The only time when Java is not creating a default constructor for you is when you explicitly declare one (with or without parameters).
Line 1 is there to trick you into believing that it is a constructor declaration but it isn't because of the void return type (a constructor should not return anything) and this method is never called in your program. A call to it should look like
...
MyClass(5);
...
So on line 2 the default constructor is called and on line 3 a constructor that takes a long is called but this constructor was never declared.
Pallavi, to make it more clear, if you leave line 2 from the original code in your modified program then that line would not compile because by declaring:
MyClass(long param)// change is made here.
you prevent the compiler from generating the default constructor and now if:
MyClass a,b;
a = new MyClass(); // 2
then line 2 won't compile.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Making some changes to the code we can compile it

This also will help to clarify the concepts that previous posters have enlisted.
 
Sri Rao
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks pallavi
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SriSri Sri,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic