permaculture playing cards*
The moose likes Java in General and the fly likes Function Outside the Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Function Outside the Class" Watch "Function Outside the Class" New topic
Author

Function Outside the Class

Amal Gupta Java Guy
Greenhorn

Joined: Feb 10, 2003
Posts: 2
Is it possible to write a java function outside the class?
Let me illustrate here:
ex:
class test
{
int a,b;
boolean equals(Test o)
{
//body of equals
}
}
here definition of fun equals is written inside the class test.
Ques --- but i want to write the definition of fun equals outside the class
becos it's very long.
How can i do this.
in c++ i can write
class test
{
int a,b;
boolean equals(Tet o);
}//end of class test
boolean test::equals()
{
body of fun
} :roll:
Vijayakumar Arya
Ranch Hand

Joined: Jan 27, 2003
Posts: 76
Hi,
Java didnot allow such conventions. You have to declare all the methods within the same class definition.
Also you cannot have a free floating function - global function as in c++, in Java, even for a static method it should be contained in a class.


Thanks,<p>Vijay<p>The Hand that gives, Gathers.
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
well...not totally. You can't do it at compile time, but you can do it on the fly. You can add bytes to a class on the fly. If you know the Java Virtual machine spec, you can do it, but i think you might also be able to do it with Apache's BCEL: http://jakarta.apache.org/bcel/index.html
Not exactly the same as what you're thinking of in C++, but pretty close. There are also other options. Make your class abstract, and don't implement that method. make an interface. But, yeah, you could create the byte-code for a method and tie it to a class at runtime.
Vijayakumar Arya
Ranch Hand

Joined: Jan 27, 2003
Posts: 76
Hi Robert,
The argument is that whether the Java as a language supports such as feature and not whether there is an alternate way to acheive that.
Please don't scare me by saying some bytecode can be attached at runtime.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Amal Gupta Java Guy,
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each and not be OBVIOSLY FICTICIOUS. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy


"JavaRanch, where the deer and the Certified play" - David O'Meara
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
Vijayakumar,
1. I'm not scaring you, it's a great feature!
2. The addition of bytecode IS the java language.
The Java language/runtime allows this and has it as a part of its features. It's a great thing, and frankly I don't see how your C++ version is any less scary or insecure as adding bytecode. Why can't I come in with my own C++ class in the same namespace and make an implementation of this method?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Before resorting to something like that, why do you feel you need to have a function outside of a class?
I'm sure that there is a simple design solution that doesn't involve scary features.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
wood burning stoves
 
subject: Function Outside the Class
 
Similar Threads
Using Objects as Parameters
JavaRanch question #208: private variable
Using Objects as Parameters
Object and String
Class Already Defined