Reputation: 459
I'd define myself as a beginner in Java, had it for just one semester and before that I had very little experience with programming whatsoever, virtually none with OOP.
Anyway, I'm going through a code and I found a method declared as a class type
public Polica stavi (Predmet p, int i)
throws GPolIndeks, GPolZauzeto, GPolTezina{
if(i<0 || i>=niz.length) throw new GPolIndeks (i);
if(niz[i] != null) throw new GPolZauzeto (i);
if(q + p.Q() > maxQ) throw new GPolTezina (p);
niz[i] = p;
q += p.Q();
return this;
}
Now the code is rather simple and almost I'm not stranger to it, except for the part where a method called "stavi" is declared. I've been thought there are two types of methods, those who return a value and those who don't, and this one does, but it is not declared as an any type regularly used (int, double, long...), it is declared with a class name, which in this case would be "Polica". This is the first time I'm coming to something like this and it works in a compiler, so my question would be, where can I read up on methods more in more detail, to better understand how this works.
Upvotes: 1
Views: 250
Reputation: 19347
Ok, I will explain some points for you. And its all about OOP
Polica
is a class which will became an Object
as soon as you create an instance of it
Polica polica = new Polica();
and methods can return any type of variables and also Objects
which means in your function you're expecting to return an Object Polica
public Polica stavi(){
Polica polica = new Polica();
return polica;
}
and this
represents its self instance of its own so it would be really same with this
public Polica stavi(){
Polica polica = new Polica();
return this;
}
Well its nice that you have a great curiosity! here's a good tutorial for you.
http://docs.oracle.com/javase/tutorial/java/concepts/index.html
http://docs.oracle.com/javase/tutorial/java/javaOO/objects.html
Goodluck!
Upvotes: 1
Reputation: 249
The official java tutorials are a great place to start with more-ever I would recommend books like Head First Java which are really good for java beginners and will help you get a grip on java fundamentals.
Being Specific to your question java methods can return either primitives like int,float etc.,inbuilt Class object instances like Integer , or custom classes that you create in your java application like "Polica " which you have mentioned above.
Also note that within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using 'this'.So in your case when your method returns 'this' and method signature says it returns 'Polica' that means that the method "stavi" is part of the Class 'Polica'and is refering to the current instance of 'Polica'.
Upvotes: 0
Reputation: 59343
it is not declared as an any type regularly used (int, double, long...), it is declared with a class name
There is no difference between int, double, long, etc. (which are called primitives) and classes when you return them. They return the same way, and a different type is returned.
public Potato weirdMethod(Elephant e) {
System.out.println(e);
return new Potato();
}
That would work fine if you defined the classes "Potato" and "Elephant."
(Edit: as Chris McCabe clarified in the comments, when you return an object it is pass-by-reference, so you can change it and it will change the actual object you returned. Primitives are pass-by-value so if you get a primitive from a method and change it, the original will be unaffected.)
In this case, looks like you want to be using a constructor. http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
Upvotes: 0
Reputation: 61
Take a look at the documentation here
The return type does not have to be a int, long or double etc. It can be of an object you define yourself.
Upvotes: 0
Reputation: 8528
here you can find what you need. and answering to your question this method returns an object of type Polica.
Upvotes: 0
Reputation: 3986
In java or any other oops language the return type of a method can be any object type i.e it is not restricted to be of primitive type ( int, double...)
You should read this (language specification) for more details.
Upvotes: 0
Reputation: 533870
I would start with the Java Tutorials for Methods
In this example, the method is returning a reference to an object which in this case is the object you just invoked a method on. This is used for chaining and a class where is often using is StringBuilder eg.
String text = new StringBuilder().append("Hello, the time is ")
.append(new Date()).append("\n).toString();
As you can see, this works because each append method return this
of the original StringBuilder.
Upvotes: 0