aspose file tools*
The moose likes Beginning Java and the fly likes static factory method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static factory method " Watch "static factory method " New topic
Author

static factory method

sivaramp pratapa
Greenhorn

Joined: Jul 05, 2003
Posts: 11
This is an example in Joshua Bloch's Effective Java..
In the following bool method, the return type is Boolean
but the return statement is returning a variable of the Boolean class
how does it compile and work ?
please explain
public class BoolTest
{
public static void main(String args[])
{
System.out.println("whats the value :"+BoolTest.bool(true));
}
public static Boolean bool(boolean a)
{
return a?Boolean.TRUE:Boolean.FALSE;
}
}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Huh? I don't recall that on the Sun Certified Java Developer assignment. I think you want to post that in a different forum.
But here is an answer, the code is correct
BoolTest.bool(true)
calls the bool method passing it a boolean. If then compares it against constants in the Boolean class.
The println() call will take the TRUE or FALSE and convert it to a string to print out.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
sivaramp pratapa
Greenhorn

Joined: Jul 05, 2003
Posts: 11
so, you are trying to tell me, that when a variable is passed to a function as a parameter, it does some thing and returns an output.
As i have understood with your answer, seems like you dint even try to understand the question!
since, this was an example for a static factory method, i thought a forum which talks about advanced java is the right choice for this question.
my question was
how does function return an instance of Boolean class!
with a return statement working on variables of Boolean class.
This is not the first time,i got such an answer from the Bartender.
But any way thanks for atleast trying to guide me !!
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Originally posted by sivaramp pratapa:

how does function return an instance of Boolean class!
with a return statement working on variables of Boolean class.

Here's the API for the variables you are pointing from Sun:

FALSE
public static final Boolean FALSE
The Boolean object corresponding to the primitive value false.
TRUE
public static final Boolean TRUE
The Boolean object corresponding to the primitive value true.

So I think it should be clear now why they are returning an instance of Boolean class.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11280
    
  59

Hi Sivaramp,
Perhaps you should rephrase your question if an answer doesnt meet your requirements.

Originally posted by Sivaramp:
In the following bool method, the return type is Boolean
but the return statement is returning a variable of the Boolean class

As Satish pointed out, the fields Boolean.FALSE and Boolean.TRUE return a Boolean object corresponding to the primitive object FALSE or TRUE respectively.
So it is returning the correct object as specified in the method signature.

Originally posted by Sivaramp:
since, this was an example for a static factory method, i thought a forum which talks about advanced java is the right choice for this question.

Perhaps a forum which talks about Java in General (advanced) might be appropriate for your question (assuming you were actually asking about factory methods or something advanced), but this forum is for people to discuss the Sun Certified Java Developer (SCJD) assignments and exams. Since this question seems to be about return types of the wrapper classes to the primitives, I have moved this thread to Java in General (beginner).
You can now find this thread here.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static factory method
 
Similar Threads
Language Fundermentals
Beginner Question
Refering to another class's instance of a different class, inside a class
operator and assignment
Boolean Output?