This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

An interesting Question on default constructor

 
Greenhorn
Posts: 12
Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yesterday i went through a test and i was asked a question
Question was

We are given a class :
public class Test{}
What is the default constructor for this class?
A. Test()
B. Test(void)
C. public Test(void)
D. public Test()

I know A and B option are incorrect
Which one should be the correct one out of options C and D?
 
Greenhorn
Posts: 10
Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A is the correct answer. You can check B and C as incorrect by writing a small code snippet. D is incorrect because visibility for default constructor is not public, it is default i.e. package visibility

Compile following code to see why B and C are incorrect
 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Devendra
Given class is declared public. So shouldn't be visibility of its default constructor also public?
 
Devendra Mahra
Greenhorn
Posts: 10
Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to miss that. You are correct. the default constructor in this case will be public since the class is public
 
Ranch Hand
Posts: 44
Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sharma,

You can try with the 'javap' or' javap -c' utility to read the byte code.
Sun says: The Java SDK from Sun includes the javap disassembler, that will convert the byte codes into human-readable mnemonics.

I have given a try for sample Test.java class which you provided.

H:\msripad>javac Test.java

H:\msripad>javap Test
Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
}


H:\msripad>javac Test.java

H:\msripad>javap Test
Compiled from "Test.java"
class Test extends java.lang.Object{
Test();
}

H:\msripad>javap -c Test
Compiled from "Test.java"
class Test extends java.lang.Object{
Test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>")V
4: return

}

 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Murali
I also tried the same code with disassembler but it is not showing public keyword with constructor in both cases-


H:\msripad>javac Test.java
H:\msripad>javap Test
Compiled from "Test.java"
public class Test extends java.lang.Object{
Test();
}


H:\msripad>javac Test.java
H:\msripad>javap Test
Compiled from "Test.java"
class Test extends java.lang.Object{
Test();
}
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see the public constructor using javap.

PublicTest.java



javap output

 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this code-


javap output-


Compiled from "Test.java"
public class Test extends java.lang.Object{
Test();
}
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should remove the default access constructor Test() from the source file and then try it again.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah...okey...I put it by mistake
thanks
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good read about Object Initialization.

Object Initialization in Java
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic