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?

[218 byte] By [Raghu_nca] at [2007-11-27 11:45:47]
# 1

NFI what you are asking. Can you rephrase your question.

floundera at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 2

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.

petes1234a at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 3

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".

Raghu_nca at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 4

Methods can return objects.

public Object something()

{

return new Object();

}

public String somethingElse()

{

return "AAAAAAAAAAAAAAAAHH!!";

}

CaptainMorgan08a at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 5

So in your code example the method must return an object of the Test class.

Test Incrtemp(){

-

}

floundera at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 6

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;

}

.............

.............

.............

}

petes1234a at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 7

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;

}

floundera at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 8

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

petes1234a at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 9

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?

jdolphina at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 10

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..

Raghu_nca at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 11

> 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.

floundera at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 12

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.

Raghu_nca at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 13

> 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

CaptainMorgan08a at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 14

Thank you .. I got the point finally...

Raghu_nca at 2007-7-29 18:03:46 > top of Java-index,Java Essentials,New To Java...
# 15

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.

floundera at 2007-7-29 18:03:51 > top of Java-index,Java Essentials,New To Java...
# 16

ok,, then here what would be the type of class Test then? is it int ?

Raghu_nca at 2007-7-29 18:03:51 > top of Java-index,Java Essentials,New To Java...
# 17

?

Test is your class. Let me repeat that . IT IS A CLASS!

floundera at 2007-7-29 18:03:51 > top of Java-index,Java Essentials,New To Java...
# 18

> 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.

jverda at 2007-7-29 18:03:51 > top of Java-index,Java Essentials,New To Java...
# 19

> Why would it be int?

Because when you don't have a friggin clue, you just make guesses.

floundera at 2007-7-29 18:03:51 > top of Java-index,Java Essentials,New To Java...
# 20

no.. i meant the return type of class..

Raghu_nca at 2007-7-29 18:03:51 > top of Java-index,Java Essentials,New To Java...
# 21

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.

floundera at 2007-7-29 18:03:51 > top of Java-index,Java Essentials,New To Java...
# 22

I had understood.. but u had confused the whole thing.....

Raghu_nca at 2007-7-29 18:03:51 > top of Java-index,Java Essentials,New To Java...
# 23

> 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.

cotton.ma at 2007-7-29 18:03:51 > top of Java-index,Java Essentials,New To Java...