In this code the main method calls the Scanner first and then calls the x() method.How can I call the x() 1st and then Scanner? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

In this code the main method calls the Scanner first and then calls the x() method.How can I call the x() 1st and then Scanner?

import java.util.*; public class Main { public static void main(String[] args) { program x = new program(); x.x(); } } class program { Scanner x = new Scanner(System.in); void x(){ System.out.println("hi"); } String b = x.nextLine(); }

16th May 2019, 11:55 AM
benny deuel
benny deuel - avatar
2 Answers
+ 2
Use this: void x(){ System.out.println("hi"); String b = x.nextLine(); } But if i were you i should rename method name, the scanner reference variable to something else, if you do bigger projects and nameing it like that.. it will really confuse you
16th May 2019, 1:25 PM
JavaBobbo
JavaBobbo - avatar
+ 2
This would be helpfull void x(){ System.out.println("hi"); String b = x.nextLine(); }
16th May 2019, 1:51 PM
Abdihafid Muhyadin Daud
Abdihafid Muhyadin Daud - avatar