Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Function Outside the Class

 
Amal Gupta Java Guy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic