• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileOutputStream question  RSS feed

 
Alan Flynn
Greenhorn
Posts: 5
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is from the android developers page



I dont understand the 4th line,shouldnt it be something like
Then use something like
I know I am wrong but I want to know why??
Thanks

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OpenFileOutput(...) will be a method that returns a reference to the FileOutputStream. That reference is being assigned to the fos variable.

I don't know what the contents of that method are, but it will be calling the FileOutputStream constructor for you (or perhaps a subclass of FileOutputStream designed to work with Android).
 
Alan Flynn
Greenhorn
Posts: 5
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you can use methods like that?Without creating an object of the class first??
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can use methods like that. Variables can be assigned any reference to an object of a compatible type. Methods can return references to objects.

The method will create the object for you and return you a reference to it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Flynn wrote:So you can use methods like that?Without creating an object of the class first??

Assuming that code is in a non-static method and that openFileOutput is a non-static method in the same class (or a super class), then you do have an object.

is equivalent to writing

where this represents the current instance of the class.
 
Alan Flynn
Greenhorn
Posts: 5
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I get it now,The method is returning a reference to the object it created thats why there is no "new" keyword,it is created in the method.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There probably is a new operator, but it is hidden somewhere in the openFileOutput method.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its called factory methods... You tell the factory what you want and it creates it and delivers it to your door...

Lets illustrate:

You have choices of a product:


You have a product:


You make a choice:
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote: . . .
// The factory is at work here^^
// No new keyword is need
. . .
Because the factory method hides the new keyword where you don't notice it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!