What is the different of that?I hv an unclear concept on it!

class A{}class B{public void T(){}public A Test(){}}what is the main function on public A Test() and the different between public A Test() and public void T() ?
[230 byte] By [PaTa] at [2007-11-27 1:34:08]
# 1
void is a method where as public A test() {} is calling an object of class A
fastmikea at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 2
when call the object of class A , what will happen and what is the meaning on that?
PaTa at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 3
public void t() declares a method called t that doesn't return anything.pubilc A test() declares a method call test that returns a reference to an A.Note that by convention, method names in Java start with lowercase letters.
jverda at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 4
ok,I see...But how about using public int Test(){}instead of public A Test(){}here is the main confusion of my conception
PaTa at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 5
> ok,I see...> But how about using > public int Test(){> }> instead of > public A Test(){> }> > here is the main confusion of my conceptionmy confusion is about what you're actually asking
georgemca at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 6

public int Test(){ // method return data type int

}

public A Test(){ // method return data type A

}

public void Test(){ // method return nothing

}

rym82a at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 7

> ok,I see...

> But how about using

> public int Test(){

> }

> instead of

> public A Test(){

> }

>

The first one returns an integer, a natural number, like 42.

The second one, as you have been told, returns an A object reference.

What is your actual question?

> here is the main confusion of my conception

Let's not talk about your conception, shall we.

Lokoa at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 8

> public int Test(){ // method return data type

> int

> }

> public A Test(){ // method return data type A

> }

> public void Test(){ // method return nothing

> }

I'm not picking holes in your answer, but in the past I've seen people get confused between 'null' and 'void' in this context. it's more accurate to say that 'void' is the absence of a return type, than to say it returns nothing. I've seen people in the past struggling with code, trying to cast all manner of objects to null, in order to get their 'void' methods to return "nothing", stuff like that. sorry if I seem like a pedantic @sshole, I'm just eliminating past confusions for anyone that finds this in the future!

georgemca at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 9

> I'm not picking holes in your answer, but in the past

> I've seen people get confused between 'null' and

> 'void' in this context. it's more accurate to say

> that 'void' is the absence of a return

> type, than to say it returns nothing. I've

> seen people in the past struggling with code, trying

> to cast all manner of objects to null, in order to

> get their 'void' methods to return "nothing", stuff

> like that. sorry if I seem like a pedantic @sshole,

> I'm just eliminating past confusions for anyone that

> finds this in the future!

No problem. It would be much clearer.

rym82a at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 10

OK,then, thx for everyone reply.

I am a newbie of the java...

My question is:

Can every one give me an example for the usage of

public A Test(){

}

I know there are the return type with Test() of A class. But...can someone write some code to represent A, just everything you like. It can teach the newbie how to take advantage of A class to write Test() ?

PaTa at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 11

> OK,then, thx for everyone reply.

> I am a newbie of the java...

>

> My question is:

> Can every one give me an example for the usage of

> public A Test(){

> }

>

> I know there are the return type with Test() of A

> class. But...can someone write some code to represent

> A, just everything you like. It can teach the newbie

> how to take advantage of A class to write Test() ?

eh? 'A' can represent whatever you want it to

georgemca at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 12
I would strongly recommend you to read this tutorial http://java.sun.com/docs/books/tutorial/Example can be find here http://java.sun.com/docs/books/tutorial/java/javaOO/returnvalue.html
rym82a at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 13
class Number{ public int value=0;}class Test{public Number method(){Number nr=new Number();nr.value=9;return nr;}}
J@A@V@Aa at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 14

> class Number{

>public int value=0;

> }

>

> class Test{

>

> public Number method(){

>

>Number nr=new Number();

> nr.value=9;

>return nr;

>

>

> }

I know it's just an example, but it's A Bad Idea ™ to use the name Number for your own class. there already is one in the JDK, and not only that, it's in the java.lang package, and hence always implicitly imported. see if you can work out what this program does:

public class String {

public static void main(String[] args) {

System.out.println("hello");

}

}

georgemca at 2007-7-12 0:41:12 > top of Java-index,Java Essentials,Java Programming...
# 15
It was just an example :)Number->RealNumbers.
J@A@V@Aa at 2007-7-21 20:08:57 > top of Java-index,Java Essentials,Java Programming...
# 16

> It was just an example :)

> Number->RealNumbers.

yeh I know. but newbs have a habit of adopting example code like that verbatim, and a class called Number is just asking for trouble. don't take offence if someone here corrects you, or makes a comment on your answer, the fora work on a peer-review system is all!

georgemca at 2007-7-21 20:08:57 > top of Java-index,Java Essentials,Java Programming...
# 17
My mistake. :))I'm taking offence from such things
J@A@V@Aa at 2007-7-21 20:08:57 > top of Java-index,Java Essentials,Java Programming...
# 18
> My mistake. :))> I'm taking offence from such thingsyeh, thought so, there's no need. and you're perfectly entitled - indeed, it's your duty - to correct other members if they make a mistake. if it's valid, the guy in question won't be the least bit bothered
georgemca at 2007-7-21 20:08:57 > top of Java-index,Java Essentials,Java Programming...
# 19

> OK,then, thx for everyone reply.

> I am a newbie of the java...

>

> My question is:

> Can every one give me an example for the usage of

> public A Test(){

> }

>

> I know there are the return type with Test() of A

> class. But...can someone write some code to represent

> A, just everything you like. It can teach the newbie

> how to take advantage of A class to write Test() ?

public class A

{

public A()

{

}

public void printSomething()

{

System.out.println( "This is class A");

}

}

public class B

{

public B()

{

}

public A test()

{

A aObject = new A();

return aObject;

}

}

public class Combiner()

{

private A a;

private B b;

public static void main( String[] args)

{

Combiner combiner = new Combiner();

a = combiner.getAAndB();

a.printSomething();

}

public void getAAndB()

{

b = new B();

a = b.test();

return a;

}

}

This will output the print in class A().

radicjesa at 2007-7-21 20:08:57 > top of Java-index,Java Essentials,Java Programming...
# 20

> > OK,then, thx for everyone reply.

> > I am a newbie of the java...

> >

> > My question is:

> > Can every one give me an example for the usage of

> > public A Test(){

> > }

> >

> > I know there are the return type with Test() of A

> > class. But...can someone write some code to

> represent

> > A, just everything you like. It can teach the

> newbie

> > how to take advantage of A class to write Test() ?

>

> > public class A

> {

>public A()

> {

>}

>public void printSomething()

> {

>System.out.println( "This is class A");

>

> }

>

> public class B

> {

>public B()

> {

>}

>public A test()

> {

>A aObject = new A();

> return aObject;

>}

>

>

> public class Combiner()

> {

>private A a;

> private B b;

>

>public static void main( String[] args)

> {

>Combiner combiner = new Combiner();

> a = combiner.getAAndB();

>a.printSomething();

>

>public void getAAndB()

> {

>b = new B();

> a = b.test();

>return a;

> }

>

>

> This will output the print in class A().

no it won't. it won't compile

georgemca at 2007-7-21 20:08:57 > top of Java-index,Java Essentials,Java Programming...
# 21

> > > OK,then, thx for everyone reply.

> > > I am a newbie of the java...

> > >

> > > My question is:

> > > Can every one give me an example for the usage

> of

> > > public A Test(){

> > > }

> > >

> > > I know there are the return type with Test() of

> A

> > > class. But...can someone write some code to

> > represent

> > > A, just everything you like. It can teach the

> > newbie

> > > how to take advantage of A class to write Test()

> ?

> >

> > > > public class A

> > {

> >public A()

> > {

> >}

> >public void printSomething()

> > {

> >System.out.println( "This is class A");

> >

> > }

> >

> > public class B

> > {

> >public B()

> > {

> >}

> >public A test()

> > {

> >A aObject = new A();

> > return aObject;

> >}

> >

> >

> > public class Combiner()

> > {

> >private A a;

> > private B b;

> >

> >public static void main( String[] args)

> > {

> >Combiner combiner = new Combiner();

> > a = combiner.getAAndB();

> >a.printSomething();

> >

> >public void getAAndB()

> > {

> >b = new B();

> > a = b.test();

> >return a;

> > }

> >

> >

> > This will output the print in class A().

>

> no it won't. it won't compile

wow...There are many compile error

PaTa at 2007-7-21 20:08:58 > top of Java-index,Java Essentials,Java Programming...
# 22
I just did it out of my head, I never really write main() applications so there might be errors here and there ;)It was just used as an example, not to run
radicjesa at 2007-7-21 20:08:58 > top of Java-index,Java Essentials,Java Programming...
# 23

Working example:

A.java

public class A

{

public A()

{

}

public void printSomething()

{

System.out.println( "This is class A");

}

}

B.java

public class B

{

public B()

{

}

public A test()

{

A aObject = new A();

return aObject;

}

}

Combiner.java

public class Combiner

{

private A a;

private B b;

public Combiner()

{

a = createA();

a.printSomething();

}

public A createA()

{

b = new B();

a = b.test();

return a;

}

}

Starter.java

public class Starter

{

private static Combiner combiner;

public static void main( String[] args)

{

combiner = new Combiner();

}

}

radicjesa at 2007-7-21 20:08:58 > top of Java-index,Java Essentials,Java Programming...
# 24

> Working example:

>

>

> A.java

>

> > public class A

> {

>public A()

> {

>}

>public void printSomething()

> {

>System.out.println( "This is class A");

> }

>

>

> B.java

>

> > public class B

> {

>public B()

> {

>}

>public A test()

> {

>A aObject = new A();

> return aObject;

>}

>

>

> Combiner.java

>

> > public class Combiner

> {

>private A a;

> private B b;

>

> public Combiner()

>{

>a = createA();

>a.printSomething();

> }

>

>public A createA()

> {

>b = new B();

> a = b.test();

>return a;

>

> }

>

>

> Starter.java

>

> > public class Starter

> {

>private static Combiner combiner;

>

>public static void main( String[] args)

> {

> combiner = new Combiner();

>}

>

>

Exception in thread "main" java.lang.NoClassDefFoundError: Starter

PaTa at 2007-7-21 20:08:58 > top of Java-index,Java Essentials,Java Programming...
# 25
no such error here
radicjesa at 2007-7-21 20:08:58 > top of Java-index,Java Essentials,Java Programming...