Posts

Showing posts from September, 2018

Android EditText getText always return the first text?

The other day, I had this weird bug on my Android project. I couldn't find some helpful information on the Internet. I decided to make a post about this weird bug. First, I create an EditText variable and initialize it in OnCreate(). //... private EditText search_bar; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_class_list); search_bar = (EditText) findViewById(R.id.search_bar); generate_class_list(db); } // I think I can get the latest value of the editText in the onClick method // But it turned out I can't get the updated value. // No matter how I change the text in the editText, it always gives me the first value public void onClick_to_search (View view) { search_bar = (EditText) findViewById(R.id.search_bar); String keyword = search_bar.getText().toString().trim().toLowerCase(); } It make me really confused in the beginning. After half an hour of debuggi...

Can a Java class have more than main methods?

Can a Java class have more than one main methods? The answer is YES. A class in Java can have more than one method, as long as the other main method has a different signature. See the example below. public class Main { public Main() { } public static void main(String[] args) { System.out.println("Main method, the entry point of the program!"); int a = main(1); int b = main(); new Main().Main(); } public static int main(int a) { System.out.println("Other main method, returning an integer!"); return 1; } public static int main( ) { System.out.println("Other main method without parameter, returning an integer!"); return 2; } }

Can I name a method with the same name of the class in Java?

Can a class have a method with the same name of the class in Java? The answer is yes. See the example below. Public class MyClass { public MyClass() { } public void MyClass() { System.out.println("I have the same name as the class!"); } } You could definitely name a method with the name of the class. As a rule of thumb, you should not capitalize the first letter of the name of your method, though.