+ 6

Is it possible to call a line in java?

Not sure if that the right word for it but i was wondering if its possible to call a certain line in the program? example 1 System.out.println("hello"); 2 3 4 5 6 7 call what ever is written on line 1 from here without using a variable? thanks

17th Oct 2017, 8:14 PM
D_Stark
D_Stark - avatar
6 Answers
+ 9
not really, put it inside a method The Java keyword list specifies the goto keyword, but it is marked as "not used".
17th Oct 2017, 8:20 PM
Kamil
Kamil - avatar
+ 8
You can imagine that each line you've written above is like an instruction to tell the compiler what to do. If you wish to carry out the same instruction multiple times without repetition, wrapping it inside a routine (method) would be the way to go. (Quantallax has provided an exellent explanation about this šŸ˜‰)
18th Oct 2017, 2:36 AM
Zephyr Koo
Zephyr Koo - avatar
+ 7
It's not possible just copy paste it or put it inside a method and call it.
18th Oct 2017, 6:48 AM
qwerty
qwerty - avatar
+ 4
thanks guys i know how to call a method juat wondering if you could call a line šŸ˜œ
18th Oct 2017, 7:14 AM
D_Stark
D_Stark - avatar
+ 3
If you are curious on how to write a method, or what it is, let me try to help: What is it? A method is a named block of code, that, upon being called, asks for certain parameters, and returns a given value. Methods can do anything you want them to, and they exist as a convenient way of preventing duplicate code and performing operations. How do you make a method? A method follows the following skeleton; <access modifiers> <object-based or static> <return type> nameOfMethod(parameters){ //code } The access modifiers determine who / what can access the method. If the method is object-based, it must be called from an object (i.e. "new Cat().meow();"). Otherwise, it may simply be called from the class (i.e. "Math.pow(2, 3);", where "Math" is a class). The return type determine what you get from calling the method (i.e. "... int plusFive(..." would return an integer type). An important side note is that "void" return types do not actually return anything. The name is simply what you call the method by (In "Math.pow(2, 3);", the method "pow" is being called). The parameters are variables passed in when the method is called, used however the method is programmed to use them (in the case of "Math.pow", the first double is the base, and the second double is the power to which the base is raised). Everything within the curly brackets is the implementation of the code, and is what is run when the method is called.
17th Oct 2017, 8:34 PM
Quantallax