Class types
What are class types in java? Because I have a code like this
class Test{
int a;
Test (int i){
a-i;
}
Test Incrtemp(){
-
}
Does it mean data type of method?
What are class types in java? Because I have a code like this
class Test{
int a;
Test (int i){
a-i;
}
Test Incrtemp(){
-
}
Does it mean data type of method?
your code is confusing to say the least... a - i; ? nothing assigned anything?
and about class types,... are you talking about return type of a method? Please clarify your question.
No I mean.. I read that methods can return return class types.. I want to know what "class types" mean in this context..
actually the it was "a=i".
Methods can return objects.
public Object something()
{
return new Object();
}
public String somethingElse()
{
return "AAAAAAAAAAAAAAAAHH!!";
}
Say you create a class called Student:
public class Student
{
private String id;
private String lastName;
private String firstName;
private double gpa;
public Student(String id, String lastName, String firstName,
double gpa)
{
this.id = id;
this.lastName = lastName;
this.firstName = firstName;
this.gpa = gpa;
}
public String getId()
{
return id;
}
...............
...............
...............
}
You could easily create a method that will return an object of the Student class type
import java.util.ArrayList;
import java.util.List;
public class StudentList
{
private List<Student> students = new ArrayList<Student>();
public boolean add(Student s)
{
return students.add(s);
}
// this method returns an object of the Student class type (or null)
public Student getStudent(String id)
{
Student tempStudent = null;
for (int i = 0; i < students.size(); i++)
{
if (id == students.get(i).getId())
{
tempStudent = students.get(i);
}
}
return tempStudent;
}
.............
.............
.............
}
At the risk of starting another "methods should only have one return statement" debate, I prefer:
public Student getStudent(String id) {
for (int i = 0; i < students.size(); i++) {
if (id == students.get(i).getId()) {
return students.get(i);
}
}
return null;
}
you say potato and I say potahto...
but more importantly, I still wonder if we're even close to answering the OP's question. Did he mean class type in a more reflection sort of way?
Message was edited by:
petes1234
Raghu_nc :
1. Classes are a blueprint of object you create. So if your method
returns an object of class Test, it means it will be calling functions and
variables defined in the class Test, not considering inheritance,etc here.
2. By writingTest (int i){
you declare a constructor of the class Test. It is called whenever an object of the class is created in the
code by writing for example
Test testObject = new Test(911)
3. I didnt understand what you mean by :
"data type of method"
There is no datatype of a method. We have return type of a method. Is that what you meant by it?
ok i will give the entire program:
class Test{
int a;
Test (int i){
a=i;
}
Test incrByTen(){
Test temp=new Test(a+10);
return temp;
}
}
Class RetOb{
public static void main(String args[]){
Test ob1= new Test(2);
Test ob2;
ob2= ob1.incrByten();
System.out.println("ob1.a : "+ob1.a);
System.out.println("ob2.a : "+ob2.a);
ob2=ob2.incrByTen();
Syste.out.println( ob2.a increased to :" +ob2.a);
}
}
we have used a function incerByTen() after Test. I couldn't understand..
> I couldn't understand..
Neither can we mate. Unless you can provide a clear explanation of your problem, we will not be able to help you.
My main doubt is:In the code Test is a class with a constructor. Now we have used the statement Test IncrByten(). I want to know what the statement implies.
> My main doubt is:In the code Test is a class with a
> constructor. Now we have used the statement Test
> IncrByten(). I want to know what the statement
> implies.
It implies that you have a method called "IncrByten" that returns a "Test" object.
http://java.sun.com/docs/books/tutorial/java/javaOO/index.html
OMG, you really need to go back and review the basics.
Its a method that has a return type of Test.
public void methodName(parameters) {
}
Here the return type is void which means return nothing. If you replace that with a primitive or a class name then the method must return a value of that primitive or a reference to an object of that class.
> ok,, then here what would be the type of class Test
> then?
No, it's Test.
> is it int ?
Why would it be int? No, it's not int. It's Test.
FFS
Go and read your notes/book/online tutorials. You do not have a clue and I for one am not interested in holding your hand.
> I had understood.. but u had confused the whole
> thing.....
Ah yes, the old blame flounder and jverd ploy.
This will turn out to be another mistake on your part.