0

since these methods are static i can either call it from an instance of HBox or directly. The question is how do i do it directly? I see examples like :

instead of using : HBox hbox = new HBox(); hbox.setMargin(btn1, new Insets(10));

i should be using: Hbox.setMargin(btn1, new Insets(10));

and hBox.setMargin(btn1, new Insets(10));

and yet HBOX.setHGrow(spacer, Priority.ALWAYS); .

So can i use any made up name infront of the dot operator and it would work?

It sure does not until I type "HBox" and then my IDE asks me to assign return value to the new variable and makes up the following code :

HBox setMargin = HBox.setMargin(btn1, new Insets(10));

This is defeating the purpose of making the methods static. Can anybody help me with this?

Ishan Srivastava
  • 1,129
  • 1
  • 11
  • 29
  • `HBox.setMargin(...)` is the correct version; since it's static you don't have to (and shouldn't) call the instance you created, it will apply to them too. And no, you can't use 'any made up' name, since it has to reference the class. – Cath Jul 14 '17 at 11:47
  • so how should i be calling these methods? – Ishan Srivastava Jul 14 '17 at 11:48
  • 1
    It's java 101 and AscendedKitten basically posted the solution: `HBox.setMargin(btn1, new Insets(10));` (without the need of creating a `HBox` instance named `HBox`) – fabian Jul 14 '17 at 11:59
  • Please read my question properly. My IDE is forcing me to create an instance setMargin. Also the 3 eg. i quote are from 3 different sources and none of them create an instance of that name before those statements – Ishan Srivastava Jul 14 '17 at 12:05
  • I have had the same problem all my life and I just create an instance and refer to it. Use this workaround until you get a legit answer. I have noticed them having weird names to reference these methods so I will contact the authors about this –  Jul 14 '17 at 12:08
  • @fabian please help me with this – Ishan Srivastava Jul 14 '17 at 17:09
  • @fabian help me with this – Ishan Srivastava Jul 19 '17 at 09:06

0 Answers0