Why java so confusing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why java so confusing

What is this in java Classname.method( new class(String A){ // Is there class override } );

8th Aug 2020, 2:33 PM
Peace Imion
Peace Imion - avatar
3 Answers
+ 2
<class name>.method() is used within reflections. But the notation on your code snippet looks strange for me. Btw. reflections are no stuff for beginners.
8th Aug 2020, 2:50 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
if you mean: new ClassName2("some string"){} it is the declaration of the anonymous class derived from the class ClassName2. It passes data to a constructor. In the example below it also extends getData() method. public class Program { public static void main(String[] args) { ClassName.method( new ClassName2("some string") { String getData() { //overriding method System.out.println(data); return data; } } ); //.method(); } } class ClassName { static void method(ClassName2 obj){ String s = obj.getData(); } } class ClassName2 { String data; ClassName2(String data){ this.data = data;} String getData(){ return data; }; }
8th Aug 2020, 11:39 PM
zemiak
0
Thanks zemiak finally got it
10th Aug 2020, 8:48 AM
Peace Imion
Peace Imion - avatar