Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

methods in Java vs functions in Kotlin

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I see a method in cannot exist independantly outside a java class. I don't know what prevents methods to exist independently? (i'm thinking, language is designed to be function as objects only and when method is called, its bound with data and object reference. - Please correct me if am wrong)

On the otherhand kotlin, function can exist independently. i.e outside the class. why is this and how is this happening?
 
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

milind k das wrote:Hello,

I see a method in cannot exist independantly outside a java class. I don't know what prevents methods to exist independently? (i'm thinking, language is designed to be function as objects only and when method is called, its bound with data and object reference. - Please correct me if am wrong)

On the otherhand kotlin, function can exist independently. i.e outside the class. why is this and how is this happening?



Welcome to the Ranch.
1. It is true that Java does not allow you to define functions outside a class.  The term method usually implies being a class member in languages that allow you to have both free-standing functions and member methods.
2. Some people aren't big fans of them, but it is perfectly legal to have static methods in classes.
They are "scoped to a class", and are part of it, but do not have any object or instance associated with them.
They can access static member variables and methods, and if they create or are passed a reference to an instance of an object can call any instance methods on those references.
They have no "this" reference, and no object need be created to call them if they are public.
3. Most earlier languages that allowed OOAD as an option allowed you to define functions outside of any classes.
Java considered itself "highly object-oriented" and at the time that meant "only member methods, no free-standing functions".

This seemed a bit restrictive to many, sometimes a function is just a function, so Kotlin decided not to keep that restriction.
 
Marshal
Posts: 74398
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . sometimes a function is just a function . . .

And that is one of the things static methods are there for.
 
Jesse Silverman
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jesse Silverman wrote:. . . sometimes a function is just a function . . .

And that is one of the things static methods are there for.



I am fan of static methods when they don't logically correlate to a particular instance.  I mentioned that some people seem slightly allergic to or distrustful of them because I consider some of those people better programmers or smarter than myself, but what you said summarizes my personal feeling about static methods.

I do often mention that they can't access instance member variables unless they are passed a reference to an instance or create one themselves, because some newbies keep hearing "they can't access instance members or methods" and don't realize that sure they can, if they have a reference to an actual object of the class, by being passed one or calling the constructor.  That, and access to any private static members or other static methods differentiates them from "standalone functions".  Lastly, both classes and interfaces can have private static methods for their own use that are "invisible" outside the class.

Lambdas in Java take care of a high percentage of the times you want some simple function that doesn't need to have a whole class defined around it.
Before that, there was a lot more annoyance of "having to define a class just to get a simple function, because Java loves Object-Orientation".

In Python, lambdas are very restricted in what they can do compared to a "free function", but who cares, you can easily define a new standalone function with a few keystrokes anywhere.
In Java, you don't have standalone functions, but have lambdas for most simple cases and static methods in classes for anything that starts getting more involved.
You can even make big fat lambdas filled with all sorts of stuff, but that is a bad idea.
Venkat S. often says "lambdas should be little and cute, if they start getting bigger just go ahead and make them a method."  I think I agree.
 
Sheriff
Posts: 16719
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

milind k das wrote:On the otherhand kotlin, function can exist independently. i.e outside the class. why is this and how is this happening?


From what I understand, Kotlin will still generate a class behind the scenes and make the top-level function a static method of that class.

https://medium.com/@brcprasanna/top-level-functions-in-kotlin-100abed5d616
 
Jesse Silverman
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been using JShell regularly but superficially since I started preparing for the OCJP.

I decided this morning to dig a little deeper and see if there aren't more things I could productively do in JShell without writing/saving/compiling a class.
There are!

Interestingly, it seems JShell is doing almost the same thing as Kotlin, which is why it superficially appears you can write "free-standing" functions in JShell:
https://www.youtube.com/watch?v=rIq32Uu6Q_s&list=PLqq-6Pq4lTTZh5EDIPZuaD3S25z49Rodz&index=9

They just get wrapped up in a class as static methods, which you can often ignore for practical purposes while messing around to learn stuff in JShell.

Last night I didn't know, this morning I do.
 
Campbell Ritchie
Marshal
Posts: 74398
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The more you try out code on JShell, the better, I think.
 
Saloon Keeper
Posts: 24598
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know that it was every laid down in stone, but "method" conventionally means a function that exists solely inside a class context (OOP). The terms "function" and "procedure" refer to non-class environments ("procedural"). In Fortran a function had its mathematical meaning - an algorithm/formula that (usually) took in 1 or more inputs and returned a value, whereas "procedure" did not return values (equivalent to a method declared returning void). And by "returning", I specifically mean not via out-of-band channels such as going back through calling parameters or via global variables.

While Java is considered to be an OOP language, I'd actually leave that term to languages like C++, which have the option to use objects, but not the obligation - that is, they can be completely procedural if they want. Java, on the other hand, can NEVER be strictly procedures, so I submit that the more accurate description for Java is Object-based.

As for static methods, well, I've discussed elsewhere how I learned slowly and painfully to use them with extreme care.

 
Junilu Lacar
Sheriff
Posts: 16719
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:I don't know that it was every laid down in stone, but "method" conventionally means a function that exists solely inside a class context (OOP). The terms "function" and "procedure" refer to non-class environments ("procedural"). In Fortran a function had its mathematical meaning - an algorithm/formula that (usually) took in 1 or more inputs and returned a value, whereas "procedure" did not return values (equivalent to a method declared returning void). And by "returning", I specifically mean not via out-of-band channels such as going back through calling parameters or via global variables.


Sounds like you and I are from the same (old) school
 
Jesse Silverman
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:
While Java is considered to be an OOP language, I'd actually leave that term to languages like C++, which have the option to use objects, but not the obligation - that is, they can be completely procedural if they want. Java, on the other hand, can NEVER be strictly procedures, so I submit that the more accurate description for Java is Object-based.
...



Whoa there, be careful because back when you and I would look at each other from opposite sides of the Atari ST / Amiga divide (yeah, I was jealous of the extra colors, stereo sound and a few other things, but the stupid PS/2 machines at engineering school could read my disks without a problem at least, which was extremely important)...

people used to use the term "Object-Based" for languages that allowed you to define classes and have encapsulation, but NO INHERITANCE AT ALL, EVEN SINGLE INHERITANCE.

I don't know that anyone is still using the term "Object-based" in contradistinction to "Object-Oriented".

I just checked and few people are, but hey, I have this wikipedia article to let us confirm I still remember the 80's very clearly:
https://en.wikipedia.org/wiki/Object-based_language

Fewer and Fewer people are making that distinction anymore, so whatever.

I did have a co-worker who used to say that Java wasn't "fully object-oriented."
"What the heck are you talking about?!  I can't even create a function outside of a class, or a variable for that matter!!"
Actually, he would come right out and say it "wasn't object-oriented" for that matter...
Why?  Because it had primitives that lived on the stack instead of being referenced objects on the heap.
Yeesh.
 
Tim Holloway
Saloon Keeper
Posts: 24598
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Offhand, I can't think of any programming languages that supported "objects" without inheritance, outside of dumb C-like data structures. Which in the circles I orbited were only "objects" in the same sense that any organized area of memory could be considered an object - not a class object (meaning member function capability). OOP dates all the way back to 1960 and I'm pretty sure that inheritance did as well.

Anyway, sorry about your disk envy. IIRC, Amiga originally only handled its own disk format, but later added  PC disk support in the OS. Mostly it was just a matter of using a dumber filesystem and providing less data capacity. It was the Macintosh that was the true nightmare. Amiga's extra capacity came from cramming more data into the outer tracks, whereas Apple actually varied the rotational speed of the drive itself.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic