VOOZH about

URL: https://www.javacodegeeks.com/2020/05/java-puzzlers-from-oca-part-6.html

⇱ Java puzzlers from OCA part 6 - Java Code Geeks


Even for new Java developers, constructors are probably no big mystery. In essence, when you create an instance of a class, the constructor of this class is started. In the 6th part of Java Puzzlers series, we will see a case related to constructors.

01
02
03
04
05
06
07
08
09
10
public class Puzzler {
    public Puzzler(){
        System.out.println("Puzzler no arg constructor");
    }
    public static void main(String[] args){
        Puzzler puzzler = new Puzzler();
    }
}

In the example above Puzzler() constructor will start and “Puzzler no arg constructor” will be printed to the screen. Now lets see a new example.

01
02
03
04
05
06
07
08
09
10
public class Puzzler {
    public void Puzzler(){
        System.out.println("Puzzler no arg constructor?");
    }
    public static void main(String[] args){
        Puzzler puzzler = new Puzzler();
    }
}

As you can see we added a return value to the constructor of Puzzler and you may expect that “Puzzler no arg constructor?” will get printed but this is not right. When we add a return value to the constuctor, it stops being a constructor. So it won’t get started when a new instance is created.

Published on Java Code Geeks with permission by Sezin Karli, partner at our JCG program. See the original article here: java puzzlers from oca part 6

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Sezin Karli
Sezin Karli
May 12th, 2020Last Updated: May 11th, 2020
0 70 1 minute read

Sezin Karli

Mathematics Engineer & Computer Scientist with a passion for software development. Avid learner for new technologies. Currently working as Senior Software Engineer at Sahibinden.com.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz