This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static methods: A.initilize().getValue(x);  RSS feed

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers
Say I have a class A
how can I do this (in the void main)?

int i = A.initilize().getValue(x);

thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks but I'm looking for the code to do this:
A.initilize().getValue(x)

just like:


String str ="somthing like this ";

int x = str.trim().length();
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can't do that because initialize() returns void (of course). For that to work, initialize() would have to return an instance of A. This would be kind of silly, as the instance wouldn't be used; furthermore, many IDEs would warn about the usage.

It's never occurred to be as a general rule before, but it's obvious that in fact static method calls can't be chained together.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


RIGHT!!!

ok...I can do this: private all methods and have one method public as:

public static int getMyValue()
{
A.initilize();
return A.getValue();
}

so in void main: A.getMyValue();
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!