Kotlin – Java Static Method equivalent in Kotlin

This tutorial shows you a way to represent Java static method in Kotlin using Object Expression.

I. Technology

– Java 1.8
– Kotlin 1.1.2

II. Practice

1. Java static Classes and Methods


public class Foo {

	public static String exe() {
		return "ozenero.com";
	}

	public static String upperCase(String s) {
		return s.toUpperCase();
	}

	public static class Bar {

		public static String bar() {
			return "Bar";
		}
	}

	public static class Num {

		public static Integer doubleInt(Integer i) {
			return new Integer(i.intValue() * 2);
		}
	}
}

2. Equivalent in Kotlin

Using Kotlin Object Expression, we can make “static” Equivalent:


class Foo {

	companion object {

		fun exe(): String = "ozenero.com"
		fun upperCase(s : String) : String = s.toUpperCase()
	}

	object Bar {

		fun bar(): String = "Bar"
	}

	object Num {

		fun doubleInt(i: Int): Int = i * 2
	}
}

3. Run & Check Result

Run the code below:


package com.javasampleapproach.staticmethod

fun main(args: Array) {

	val static = Foo.exe()
	println(static)
	
	val upper = Foo.upperCase("ozenero.com")
	println(upper)
	
	val bar = Foo.Bar.bar();
	println(bar)
	
	val num = Foo.Num.doubleInt(3);
	println(num)
}

Check Result:


ozenero.com
ozenero.com
Bar
6

86 thoughts on “Kotlin – Java Static Method equivalent in Kotlin”

  1. I am not sure where you’re getting your information, but great topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for great information I was looking for this information for my mission.

  2. magnificent points altogether, you just received a logo new
    reader. What could you recommend in regards to
    your post that you made a few days in the past? Any positive?

  3. Hi there! I just wanted to ask if you ever have any trouble with hackers?
    My last blog (wordpress) was hacked and I ended up losing months of hard work
    due to no backup. Do you have any methods to protect against hackers?

  4. Hello would you mind stating which blog platform you’re using?
    I’m planning to start my own blog in the near future but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most
    blogs and I’m looking for something unique.
    P.S My apologies for being off-topic but I had to ask!

  5. It’s a shame you don’t have a donate button! I’d most certainly donate to this excellent blog!

    I suppose for now i’ll settle for book-marking and adding
    your RSS feed to my Google account. I look forward to brand new updates and will share
    this site with my Facebook group. Chat soon!

  6. I am curious to find out what blog platform you’re working with?
    I’m experiencing some minor security issues with my
    latest site and I’d like to find something more risk-free.
    Do you have any solutions?

  7. An intriguing discussion is worth comment. I do believe that you should write more about this topic, it might not be a taboo subject but typically folks don’t speak about these subjects.
    To the next! Cheers!!

  8. Great blog here! Also your website loads up fast!

    What web host are you using? Can I get your affiliate link to your host?
    I wish my site loaded up as quickly as yours lol

  9. Great post. I was checking continuously this blog and I am impressed!
    Extremely helpful info particularly the last part 🙂 I care for such
    info a lot. I was seeking this certain information for a long time.
    Thank you and good luck.

  10. hey there and thank you for your info – I’ve definitely picked up anything new from right here.
    I did however expertise a few technical points
    using this site, as I experienced to reload the website a lot of times previous to I could get it to load correctly.
    I had been wondering if your web host is OK? Not that I am complaining, but slow loading instances times will
    very frequently affect your placement in google and could damage your high
    quality score if ads and marketing with Adwords.

    Well I’m adding this RSS to my e-mail and could look out for a lot more of your respective
    interesting content. Make sure you update this again very soon.

  11. Hello! This is my first visit to your blog! We are a group of
    volunteers and starting a new initiative in a community in the same niche.
    Your blog provided us useful information to work on. You have done
    a wonderful job!

  12. My spouse and I absolutely love your blog and find the majority of your post’s to be just
    what I’m looking for. can you offer guest writers to
    write content for yourself? I wouldn’t mind composing a post or elaborating on most of the subjects you write about here.
    Again, awesome site!

  13. Fantastic beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog site?
    The account helped me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear idea

  14. I like the valuable info you provide in your articles.
    I will bookmark your blog and check again here frequently.
    I am quite sure I will learn lots of new stuff right here!
    Best of luck for the next!

  15. Wow that was odd. I just wrote an very long comment
    but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again. Regardless, just wanted to say superb blog!

  16. Hi there! I understand this is kind of off-topic but I had to ask.
    Does building a well-established blog like yours take a massive amount work?
    I am completely new to operating a blog however
    I do write in my diary everyday. I’d like to start a blog so I will be able to share
    my experience and views online. Please let me know if you have any ideas or
    tips for new aspiring bloggers. Thankyou!

  17. Hi there terrific blog! Does running a blog like this require a massive amount work?
    I’ve very little understanding of computer programming however
    I was hoping to start my own blog soon. Anyhow, should you have any ideas or
    tips for new blog owners please share. I understand this
    is off subject but I just wanted to ask. Kudos!

  18. I like the helpful info you provide in your articles.
    I will bookmark your weblog and check again here regularly.

    I am quite certain I will learn many new stuff right
    here! Best of luck for the next!

  19. Whats up very nice blog!! Guy .. Excellent .. Amazing ..
    I’ll bookmark your website and take the feeds additionally?
    I’m satisfied to seek out a lot of useful info here within the put
    up, we’d like develop more strategies on this regard, thank you for sharing.
    . . . . .

  20. I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored subject matter stylish.

    nonetheless, you command get got an nervousness over that
    you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same
    nearly a lot often inside case you shield this increase.

  21. I simply couldn’t depart your website before suggesting that I actually loved the standard
    info an individual provide on your visitors? Is gonna be back continuously in order to inspect new posts

  22. I’m very pleased to uncover this web site. I wanted to thank
    you for your time due to this wonderful read!! I definitely appreciated every little
    bit of it and I have you book-marked to look at new stuff in your site.

  23. Hi would you mind stating which blog platform you’re working with?

    I’m looking to start my own blog in the near future but I’m having a difficult time
    deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique.
    P.S My apologies for getting off-topic but I had to ask!

  24. Wonderful goods from you, man. I have take note your
    stuff previous to and you’re just extremely magnificent. I really like what
    you have bought here, really like what you are stating and the best way in which you say
    it. You make it entertaining and you continue to care for to keep it
    smart. I cant wait to read much more from you. That is really a tremendous web site.

  25. Hello arе using Wordpess ffor your ѕite platform?
    I’m new to the blog world butt I’m trying to get started and create
    my own. Do you require aany codіnjg knowleԁge to mаke yoսr oᴡn bloɡ?
    Any help would be greatⅼy appreciatеd!

  26. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get
    three e-mails with the same comment. Is there any
    way you can remove me from that service? Thanks a lot!

  27. I do consider all of the concepts you have offered to your post.
    They’re really convincing and can certainly work.
    Nonetheless, the posts are too brief for beginners.

    Could you please prolong them a bit from subsequent time?

    Thanks for the post.

  28. Hello, I do believe your website might be having browser compatibility
    issues. Whenever I take a look at your website in Safari,
    it looks fine but when opening in IE, it’s got some overlapping issues.
    I simply wanted to provide you with a quick heads up!
    Other than that, excellent website!

  29. Hey there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Opera.
    I’m not sure if this is a formatting issue or something to do with browser compatibility but I thought I’d post to let you know.
    The layout look great though! Hope you get the issue solved soon. Cheers

  30. I just like the helpful information you provide in your articles.
    I’ll bookmark your blog and take a look at once more here frequently.
    I’m moderately certain I’ll learn many new stuff
    proper right here! Best of luck for the following!

  31. Wonderful blog! I found it while surfing around on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thanks

  32. When someone writes an piece of writing he/she retains the idea of
    a user in his/her mind that how a user can be aware
    of it. Thus that’s why this article is great. Thanks!

  33. Have you ever considered creating an e-book or guest authoring on other blogs?
    I have a blog based on the same topics you discuss and would love to have you share some stories/information. I
    know my subscribers would enjoy your work. If you are
    even remotely interested, feel free to send me an email.

  34. Good post. I learn something new and challenging on sites I stumbleupon everyday.
    It’s always helpful to read articles from other writers and practice
    something from their sites.

  35. Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point. You obviously
    know what youre talking about, why waste your intelligence on just posting
    videos to your blog when you could be giving us something informative to read?

  36. Wow, superb weblog format! How lengthy have you ever been running a blog for?
    you make blogging glance easy. The whole look of your
    web site is fantastic, as smartly as the content!

  37. We are a group of volunteers and opening a new scheme in our community.
    Your site provided us with valuable information to work on.
    You have done an impressive job and our whole community will be grateful to
    you.

  38. I think this is among the most significant info for me.

    And i’m glad reading your article. But want to remark on some general
    things, The site style is ideal, the articles is really great :
    D. Good job, cheers

  39. Have you ever considered publishing an ebook or guest authoring on other
    sites? I have a blog based upon on the same information you discuss
    and would really like to have you share some stories/information. I know my visitors would enjoy your work.
    If you’re even remotely interested, feel free to send me an e-mail.

  40. Great goods from you, man. I’ve understand your stuff previous to and you
    are just too excellent. I really like what you have
    acquired here, really like what you are saying and the way in which
    you say it. You make it entertaining and you still take care of to
    keep it sensible. I can’t wait to read much more
    from you. This is really a wonderful web site.

  41. Nice blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple tweeks would really make my blog shine.
    Please let me know where you got your design. Appreciate
    it

  42. Great site you have here but I was curious about if you knew of any forums that cover
    the same topics discussed here? I’d really love to be a part of community where I can get opinions from other experienced people that share the same interest.

    If you have any suggestions, please let me know. Kudos!

  43. Excellent blog! Do you have any tips and hints for aspiring writers?
    I’m hoping to start my own blog soon but
    I’m a little lost on everything. Would you advise starting with a free platform
    like WordPress or go for a paid option? There are so many
    choices out there that I’m totally overwhelmed .. Any suggestions?
    Many thanks!

  44. You really make it seem so easy with your presentation but I find this matter to
    be really something that I think I would never understand.
    It seems too complex and very broad for me. I am looking forward for your next post,
    I will try to get the hang of it!

  45. Wow that was unusual. I just wrote an very long comment
    but after I clicked submit my comment didn’t show up. Grrrr…
    well I’m not writing all that over again. Anyways, just wanted to say great blog!

  46. It’s really a nice and helpful piece of information. I am glad that you just shared this useful information with us.

    Please stay us informed like this. Thanks for sharing.

  47. You really make it seem so easy with your presentation but I find this topic to be really something that I think I would never understand.
    It seems too complex and very broad for me.
    I am looking forward for your next post, I’ll try to get the hang of it!

  48. What i do not realize is actually how you are not actually a
    lot more neatly-preferred than you might be now. You’re so intelligent.
    You already know thus considerably in relation to
    this subject, produced me individually believe it from a lot
    of various angles. Its like men and women don’t seem to
    be involved unless it’s something to accomplish with Lady gaga!
    Your own stuffs great. All the time maintain it up!

  49. We are a group of volunteers and starting a new
    scheme in our community. Your website offered us with valuable info to
    work on. You’ve done a formidable job and our whole community will be thankful to you.

  50. Definitely believe that which you said. Your favorite reason seemed to be on the
    net the simplest thing to be aware of. I say to you, I definitely get irked while people consider worries that
    they plainly do not know about. You managed to hit the nail
    upon the top and also defined out the whole thing without having side effect , people can take a signal.
    Will likely be back to get more. Thanks

  51. Attractive component of content. I simply stumbled upon your weblog and in accession capital to assert that I
    acquire in fact enjoyed account your weblog posts. Any way I’ll be subscribing in your augment or even I achievement
    you access persistently quickly.

  52. Hello there, I discovered your site by means of
    Google whilst searching for a related subject,
    your website got here up, it appears good. I’ve bookmarked it in my google bookmarks.

    Hello there, simply changed into alert to your weblog through Google,
    and located that it is truly informative. I am
    gonna be careful for brussels. I will be grateful if you happen to proceed this
    in future. Numerous other people can be benefited out of your writing.
    Cheers!

  53. hey there and thank you for your info – I’ve certainly picked up something new from
    right here. I did however expertise several technical issues using this site, since I experienced to reload the web site many times previous to I could get
    it to load correctly. I had been wondering if your
    hosting is OK? Not that I’m complaining, but sluggish loading instances times will very
    frequently affect your placement in google and can damage your
    quality score if ads and marketing with Adwords.
    Anyway I’m adding this RSS to my email and could look out for
    a lot more of your respective interesting content. Ensure that you update this again very soon.

  54. First off I would like to say superb blog! I had a quick question in which
    I’d like to ask if you don’t mind. I was
    interested to find out how you center yourself and clear your mind before writing.
    I have had difficulty clearing my thoughts in getting my thoughts out there.
    I do enjoy writing but it just seems like the first 10 to 15 minutes are usually lost simply just trying to
    figure out how to begin. Any ideas or hints? Many thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *