public int Test(){ // method return data type int
}
public A Test(){ // method return data type A
}
public void Test(){ // method return nothing
}
> 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.
> 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!
> 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.
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() ?
> 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
> 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");
}
}
> 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!
> 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().
> > 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
> > > 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
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();
}
}
> 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