Can You Access?

Sort by

recency

|

138 Discussions

|

  • + 0 comments

    what is wrong with my solutions?

    import java.io.*;
    import java.util.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int num=sc.nextInt();
            int a=0;
            if((num>0)&&(num&(num-1))==0){
                System.out.printf("%d is power of %d",num,2);
            }
            else{System.out.printf("%d is not power of %d\n",num,2);
            System.out.print("An instance of class: Solution.Inner.Private has been created");
            }
    
            }
    }
    
  • + 0 comments

    I came to the solution by adding the following lines to use Java reflection and access the private powerof2 method in Solution.Inner.Private:

    Solution.Inner inner = new Solution.Inner();
    Solution.Inner.Private innerPrivate = inner.new Private();
    o = innerPrivate;
    Method method = innerPrivate.getClass().getDeclaredMethod("powerof2", int.class);
    method.setAccessible(true); 
    String result = (String) method.invoke(o, num); 
    System.out.println(num + " is " + result);
    

    `

  • + 0 comments
    o = new Inner().new Private();
    String powerOf2 = ((Inner.Private)o).powerof2(num);
    System.out.println(num + " is " + powerOf2);
    
  • + 0 comments

    The task description is very vague. Only by reading Editorial section I learned that we are supposed to use reflection to call the method of inner class.

  • [deleted]
    + 0 comments

    For JAVA 8

    Inner x = new Inner();
      o = x.new Private();
        Inner.Private y = (Inner.Private) o;
    System.out.println(num+" is "+ y.powerof2(num)); 
    

    Just write it to the area that is shown us.