简体   繁体   中英

Test1 & Test2 in the same package, so why do I need import test2? if I don't use import, Inn inn1 = test2.new Inn(4), It will go wrong

Test1 & Test2 in the same package, so why do I need import test2?

if I don't use import, Inn inn1 = test2.new Inn(4), It will go wrong. && other question:

public void show(final int number){

 }

If I use final here, What' this meaning?

 package info;
    import info.Test2.Inn;

    public class Test1 {
        public static void main(String[] args) {
            int sum = 1;
            Test2 test2 = new Test2(2);
            Test2.Inn inn = test2.new Inn(3);
            Inn inn1 = test2.new Inn(4);
            for (int i = 0; i < 9; i++){
                sum = (sum + 1) * 2;
            }
            System.out.println(sum);
        }
    }
    class Test2 {
        private int test;

        public Test2(int test) {
            this.test = test;
        }


        class Inn{
            private int inn;

            public Inn(int inn) {
                this.inn = inn;
            }

        }
    }

The same package can only omit the package name.

The whole signature:

package_name.Test2.Inn
package_name.Test2

->

Test2.Inn
Test2

If use Inn instead of Test2.Inn

What if there is a class in same package with name Inn ?


The final in method signature means number cannot be modified.

So something like number++ is not allowed in the method.

You can remove import statement as follows

Test2.Inn inn1 = test2.new Inn(4);

So, entire code might be looks,

public class Test1 {
    public static void main(String[] args) {
        int sum = 1;
        Test2 test2 = new Test2(2);
        Test2.Inn inn = test2.new Inn(3);
        Test2.Inn inn1 = test2.new Inn(4);

        for (int i = 0; i < 9; i++){
            sum = (sum + 1) * 2;
        }
        System.out.println(sum);
    }


}

class Test2 {
    private int test;

    public Test2(int test) {
        this.test = test;
    }


    class Inn{
        private int inn;

        public Inn(int inn) {
            this.inn = inn;
        }

    }
}

Let me show you another case

Make Test2 inner class of the Test1 class

public class Test1 {
    public static void main(String[] args) {
        int sum = 1;
        Test2 test2 = new Test2(2);
        Test2.Inn inn = test2.new Inn(3);
        Test2.Inn inn1 = test2.new Inn(4);

        for (int i = 0; i < 9; i++){
            sum = (sum + 1) * 2;
        }
        System.out.println(sum);
    }

    class Test2 {
        private int test;

        public Test2(int test) {
            this.test = test;
        }


        class Inn{
            private int inn;

            public Inn(int inn) {
                this.inn = inn;
            }

        }
    }
}

But the code won't compile yet because you can not access Test2 class in the static method of Test1 class.

So, you can do this

static class Test2

Now, you can access static member Test2 class of Test1 and its inner class Inn by declaring Test2.Inn

public class Test1 {
    public static void main(String[] args) {
        int sum = 1;
        Test2 test2 = new Test2(2);
        Test2.Inn inn = test2.new Inn(3);
        Test2.Inn inn1 = test2.new Inn(4);

        for (int i = 0; i < 9; i++){
            sum = (sum + 1) * 2;
        }
        System.out.println(sum);
    }

    static class Test2 {
        private int test;

        public Test2(int test) {
            this.test = test;
        }


        class Inn{
            private int inn;

            public Inn(int inn) {
                this.inn = inn;
            }

        }
    }    
}

Then, the above code is a little ugly so, I can finally make my own code

import Test1.Test2.Inn;

public class Test1 {
    public static void main(String[] args) {
        int sum = 1;
        Test2 test2 = new Test2(2);
        Inn inn = new Inn(3);
        Inn inn1 = new Inn(4);

        for (int i = 0; i < 9; i++){
            sum = (sum + 1) * 2;
        }
        System.out.println(sum);
    }

    static class Test2 {
        private int test;

        public Test2(int test) {
            this.test = test;
        }


        static class Inn{
            private int inn;

            public Inn(int inn) {
                this.inn = inn;
            }

        }
    }    
}

I think it is all about scope or accessibility of class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM