• 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

practice question: what is the output?

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
=========
class Test {
public static void main(String[] args) {
Test t = new Test();
t.Test();
}
Test() {System.out.println("Testing...");}}
=========

I would think this would be a compiler error, since Test() is not a method, it is a constructor and the class Test does not have a method declared named Test().
Is this the case?
Zak Nixon
SCJP in progress
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It will not compile.Will show compilation error "cannot resolve symbol"
since Test() is a constructor not a method.
Thanks
Chandrasekhar S.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
Since the method cannot have the same name as that of the class ,this would surely give a compilation error..

If that were a in small case..case insensitive ..this would compile sure..
 
Raja Sagar Panamgipalli
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
Since the method cannot have the same name as that of the class ,this would surely give a compilation error..

If that were a in small case..case insensitive ..this would compile sure..
class Test {
public static void main(String[] args) {
Test t = new Test();
t.test();
}
test() {System.out.println("Testing...");}}
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
The code will surely give compile time error as method Test() does not exists.
But we can have a method name same as class name.
In the above example, Test() is a constructor, but if we have return type mentioned then this becomes a method and code compiles and runs fine.
Check the code below::

This compiles and the output displayed at run time is :

Having method name same as class name is not recommended but the code compiles and runs.

Thanks,
Cody.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raja Sagar

If you mean to say 'test' is a method then you should declare a return type. In this case it is 'void'.
-------------
Sainudheen
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic