标签: local-class

为什么有 2 个类型名称相似的对象时会出现 ClassLoader 异常

我有一堂有两种方法的课程。

  • 在 中method1(),我创建了一个local record名为Abc. 这local record仅适用于method1()因为它是在(这里是根据 Java 的规则)中定义的。method1()

  • 在 中method2(),我创建了一个local interface名为ABC. 请注意,这里存在大小写差异。该local record被命名了Abc,但这个local interface被命名了ABC

这是班级。

//package ..... //commenting out package information, as I sincerely doubt that is the cause

/** There seems to be a class loader error when running the below method in main(). */
public class ClassLoaderIssue
{

   /** Method …
Run Code Online (Sandbox Code Playgroud)

java identifier uniqueidentifier classloader local-class

3
推荐指数
1
解决办法
1768
查看次数

有没有叫做Local Static Inner Class的东西?

我只是在试验内部类,并且遇到了这个有本地但是静态内部类的想法......我在静态方法中创建了一个内部类......好吧,它就像那样简单..这是我做的例子

  class Outer {

    static void m() {
        class LocalStatic {

            void s() {
                System.out.println("static local inner class method");
            }

        }
    }
}

class Demo {

    public static void main(String args[]) {

        Outer.m();
    }
}
Run Code Online (Sandbox Code Playgroud)

这不会给出任何编译错误.

我知道如何访问静态方法m.但我想知道是否有办法 从外部类访问本地类LocalStatic ..那么根据我的理解,我们无法访问方法内的某些内容吗?因此,我不能访问任何LocalStatic或任何方法或类之外的本地类的内部属性只是想确保..

java static-methods inner-classes local-class

2
推荐指数
1
解决办法
68
查看次数

我们不能在本地类中定义友元函数是什么原因?

我有以下 C++ 片段代码。在main()函数内部声明一个类。

我们不能在本地类中定义友元函数是什么原因?

#include<iostream>

int main()
{
    class Foo
    {
        void foo() {} // Ok
        friend void Bar(){}; // Error
    };
}
Run Code Online (Sandbox Code Playgroud)

c++ friend local-class c++11

2
推荐指数
1
解决办法
1355
查看次数

java forward forward引用在不同情况下的行为是否不同?

请看一下这个片段:

public class A {
    void method() {
        System.out.print(B.j);//This is legal!
        class C {
            void method () {
                System.out.print(j);//This is illegal!
            }
        }
        final int j = 10;
        class D {
            void method() {
                System.out.print(j);//This is legal!
            }
        }
    }
}
class B {
    static int j = 10;
}
Run Code Online (Sandbox Code Playgroud)

我们可以在定义之前访问某个地方的'Bj',而在C类访问'final int j'的情况下这是非法的.

java编译器将本地类看作简单的变量/对象吗?特别是,这种行为背后的理由是什么?我的意思是前向检查正在为Bj工作,但它不适用于C类中的'j'.

java compiler-construction inner-classes local-class

1
推荐指数
1
解决办法
145
查看次数

为什么不能在Java中创建本地类的实例?

如果我有这个代码.

public class Test{
        {
            class People {

            }
        }

        public static void main(String[] args) {
            People person = new People();//Compile ERROR
        }

    }
Run Code Online (Sandbox Code Playgroud)

我无法创建People的实例.

这是否意味着Initializer Block不能用于定义类?

java field class local-class

1
推荐指数
1
解决办法
2133
查看次数

Java中的内部本地类

public class Main {
    public static void main(String[] args) {
        int b=1;
        final int c=2; 
        String s1[] = new String[]{"A","B","C"};

        class InnerMain{                                    
            int a=5;

            public void show(){ 
                System.out.println(s1[0]);
                System.out.println("A:" + a);
                System.out.println("B:" + b);
                System.out.println("C:" + c);
            }
        }

        InnerMain test =new InnerMain();
        test.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

我研究过的这本书说,本地类只能使用本地类所在的final变量和方法的引用.在这个例子中,我使用的b是不是final或引用的变量.它跑了,我没有得到任何错误.怎么样?有人可以解释这种行为吗?

java local-class

1
推荐指数
1
解决办法
53
查看次数

为什么内部类实例会在内存中进行remian,即使外部类对象被销毁了?

请考虑以下两个类:

一个学生

package datatypes;

public class Student {

    private String name ;

    public Student(String name) {
        this.name = name;
    }

    class Address{
        String city;
        String state;

        Address(String city , String state){
            this.city = city;
            this.state = state;
        }

        String getAddress(){
            return city + state;
        }

    }

    String getName(){
        return name;
    }
}
Run Code Online (Sandbox Code Playgroud)

b.)StudentDemo

package datatypes;

import datatypes.Student.Address;

public class StudentDemo {
    public static void main(String[] args) {
        Student obj = new Student("Yati");
        Address adr = obj.new Address("YNR" , "HARYANA");

        System.out.println(obj.getName()); …
Run Code Online (Sandbox Code Playgroud)

java garbage-collection inner-classes local-class

1
推荐指数
1
解决办法
456
查看次数

具有Lambda表达式的本地类

在我测试时,下面的代码执行没有任何问题.但我无法理解逻辑.有人可以解释一下吗?

public static void main(String[] args) {
    List<String> london = new ArrayList<>(Arrays.asList("Chinatown","Croydon","Eden Park"));
    class City{
        private String name;
        City(String name){
            this.name = name;
        }

        public void printCity(){
            System.out.println(name);
        }
    }

    london.stream().map(City::new).forEach(City::printCity); //Chinatown,Croydon,Eden Park
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例代码中,我有以下问题.

  1. foreach方法始终采用消费者对象.在这里,printCity方法不是一个接受参数的方法.它仍然有效.怎么样?
  2. printCity方法在这里不是静态方法.城市本身如何调用实例方法?

java lambda local-class java-stream method-reference

0
推荐指数
1
解决办法
102
查看次数