1

Is there any way to declare such static variables in java by which I can return java object after their initialization for example

We can do this in python like this

schedule.every(10).minutes.do(job)

Where, by calling every(10). minutes I just initialize minutes variable.

Is it possible to do such things in java too?

Something like this:

Myclass.function().variable.anothorFunction();

EDIT:

As in case of methods, I can easily do this in java too like this.

public static MyClass function(){
      return new MyClass();
}
Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
Nitin
  • 1,280
  • 1
  • 13
  • 17
  • 2
    Yes. You just return a value from the method. – Andy Turner Jun 26 '18 at 18:01
  • Please see my edits too@ErnestKiwele – Nitin Jun 26 '18 at 18:05
  • @AndyTurner No,I don't want to create method – Nitin Jun 26 '18 at 18:06
  • 1
    @NitinKhanna then there won't be a method for you to call to do what you are asking. – Andy Turner Jun 26 '18 at 18:07
  • @AndyTurner Ya, I know but like in python we can call a method after calling its variable as I mentioned in above python example. I just wanna know Is it possible in java too? – Nitin Jun 26 '18 at 18:10
  • @NitinKhanna it is unclear what you mean. There is nothing obviously outside the scope of Java that you are showing in your Python snippet. You have various attributes which have other attributes. Some of those attributes are methods which you can call, which can return other objects, with their own attributes and methods. All of this applies equally to Java and Python. – juanpa.arrivillaga Jun 26 '18 at 18:24
  • @juanpa.arrivillaga Ya, I know this as I already mentioned in my edits but what if I just call a static variable not a method like MyClass.func().variable and then again call the .anotherfunc() method, as I do in python schedule.every(), .minute and then again call .do() method – Nitin Jun 26 '18 at 18:29
  • @NitinKhanna then that variable must be `static` – Lino Jun 26 '18 at 18:30
  • @juanpa.arrivillaga In this case how can i return new object of the class? – Nitin Jun 26 '18 at 18:30
  • @Lino Ya, obviously – Nitin Jun 26 '18 at 18:32
  • 1
    @NitinKhanna then in Java, as in Python, the `.variable` attribute must return an object with your `.anotherfunc()` method. Again, I am not sure what exactly you think Python is doing here that is intractable in Java. – juanpa.arrivillaga Jun 26 '18 at 18:33
  • @NitinKhanna the same way you always do? `return new MyClass()`? – juanpa.arrivillaga Jun 26 '18 at 18:35
  • @NitinKhanna If you know that, then I am not entirely sure what you're really asking – Lino Jun 26 '18 at 18:35
  • @juanpa.arrivillaga Ummm...See As you said that .variable attribute must return an object that is my question how a variable can return an object? – Nitin Jun 26 '18 at 18:36
  • @NitinKhanna ... variables always reference objects (OK, in Java, sometimes they are primitive types, but most of the time they are objects). So, again, I am not sure exactly what it is that you don't understand here. – juanpa.arrivillaga Jun 26 '18 at 18:37
  • @juanpa.arrivillaga Okay, See my question is quite simple like as above example in python I am just calling do() methos of same class schedule after initializing minutes variable Is it possible to do this in java too? – Nitin Jun 26 '18 at 18:40
  • 1
    I am always glad to help, and your question had some interesting twists to offer, too ;) ... I also appreciate the quick accept! – GhostCat Jun 26 '18 at 18:49
  • @GhostCat hehe...Thank as per my country time good night – Nitin Jun 26 '18 at 18:53
  • Relative to your edit this kind of thing have a name : fluent API . You will often find that on unmutable object like String . But you find this kind of API on apache connections too. – Arnault Le Prévost-Corvellec Jun 26 '18 at 18:59

2 Answers2

3

I first assumed that python snippet works because python has generators and yield. Which Java doesn't have. In Java a method returns one value, and one value only. The closest that you could get on the JVM platform would be the loop/yield construct as offered by Scala. Or maybe, Java 8 Streams.

But given the comments, I had a closer look at your example, leading to this python module. The whole thing boils down to schedule.every() returning a scheduler object which simply invoked the function job passed to it, based on the duration passed in via a "fluent" interface.

You could do a very similar thing in Java, where you put a static method on some class that simply creates an instance of some Scheduler class, which you then pass a lambda/method reference for example.

But of course, Java has also built-in mechanisms to do this, for example the ScheduledExecutorService.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    Well, you can hand-craft these, returning a different kind of object with context information at each stage, but it's laborious... – T.J. Crowder Jun 26 '18 at 18:05
  • Is streams can do this? – Nitin Jun 26 '18 at 18:08
  • 1
    I don't see anything about `schedule.every(10).minutes.do(job)` that implies a generator. If anything, it implies methods that `return self` allowing for a fluent interface, something which is not generally considered Pythonic, although some libraries implement it (notably, it is generally absent from the standard library). I don;t really understand what the OP is looking for, though... – juanpa.arrivillaga Jun 26 '18 at 18:11
  • @juanpa.arrivillaga Many thanks. I reworked my answer accordingly. – GhostCat Jun 26 '18 at 18:38
  • 3
    @GhostCat one complication: looking at [the source code](https://github.com/dbader/schedule/blob/master/schedule/__init__.py#L155), the `.minute` attribute is actually a Python *descriptor*, but that is simply an object that implements a specific protocol (`__get__` and `__set__`). I'm not sure what the Java equivalent would be, but just another object would probably do, albeit without the syntactic sugary attribute-like access – juanpa.arrivillaga Jun 26 '18 at 18:40
  • 2
    @NitinKhanna note, another complication, `schedule.every` is actually a *module level* function (something java *does not have*, everything must be in a class definitions) which simply wraps a call to a `default_scheduler.every` method, where `default_scheduler` is a global `Scheduler` object plopped into the module namespace, i.e. `default_scheduler = Scheduler()` – juanpa.arrivillaga Jun 26 '18 at 18:57
0

Assuming schedule is an object-reference and not a class-reference you can do that easily by returning this after your method is finished. See this simplified example:

public class MyClass {

   public OtherClass variable;

   public MyClass function(){
       // do stuff

       return this;
   }
}

public class OtherClass {

    public void anotherFunction(){
        // do other stuff
    }
}

Which then can be used like this:

MyClass myObj = ...;

myObj.function().variable.anotherFunction();

An even better way would be to directly return the variable itself:

public OtherClass function(){
    // do stuff

    return variable;
}

Which in turn can then be used like this:

myObj.function().anotherFunction();
Lino
  • 19,604
  • 6
  • 47
  • 65