Method type as a Class name

0 Hemlata Mahaur · October 17, 2016
Here's my code:

public class Stuff() {
  private int x = 1;
  public void someMethod() {

  public Stuff secondMethod() {  // Method type as class name
  // someCode 

 My question is why do we create method type same as class name? Plus What's the benefit of using it?

Post a Reply


Oldest  Newest  Rating
0 Nicholas Eason · October 17, 2016
That's really a situational question. Usually you'll do this for a clone method, I think Android has some of these. The easiest way to clone something would be a method like this:

public Stuff clone(){
return this;

Sometimes you need to clone things, for example, cloning visual components in Android will remember their size even when you need to use it with something else.

In another use, you may have a Vector2 or Vector3 (Vector2 stores X and Y points, Vector3 stores X Y and Z points). The easiest way to represent the difference of two Vector2 variables, is by using a Vector2 variable.

In what context did you see this used? I find it's easier to explain why a single case is happening rather than the whole generic use, because there's literally infinite possibilities for why this could be used.
0 Hemlata Mahaur · October 18, 2016
I saw this used here: 
// class name is ThreadFactoryBuilder

public ThreadFactoryBuilder setNameprefix(String namePrefix) {

if (namePrefix == null) {

throw new NullPointerException();


this.namePrefix = namePrefix;

return this;


public ThreadFactoryBuilder setDaemon(boolean daemon) {

this.daemon = daemon;

return this;

0 Nicholas Eason · October 18, 2016
Okay, so that method doesn't need to return anything, it just returns in case you'd want to do something with it. In the method, it returns 'this', which will just create a copy of the object and return it. Arguably, that's bad practice.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator