public class Test {
class Foo {
int frob() {
return 7;
}
}
class Bar extends Foo {
@Override
int frob() {
return 8;
}
}
class Baz extends Foo {
@Override
int frob() {
return 9;
}
我正在解决一些Java难题,偶然发现了这个:
public class Outer {
class Inner1 extends Outer {}
class Inner2 extends Inner1 {}
}
在用javac 1.6.0_45编译这段代码时,不出所料,我得到了这个错误:
Outer.java:8: cannot reference this before supertype constructor has been called
class Inner2 extends Inner1 {}
我们知道静态上下文不能引用任何类型的任何实例,但是main方法发生了什么,下面的代码示例如何编译没有问题:
public class MyOuter
{
public static void main(String[] args)
{
MyOuter mo = new MyOuter(); // gotta get an instance!
MyOuter.MyInner inner = mo.new MyInner();
inner.seeOuter();
//Or
MyOuter.MyIn
在java中如何在静态类中声明内部类的对象?
public class xyz
{
static class abc
{
...
// I want to declare an object of class a here. how can I do this?
}
class a
{
...
}
}
当实例化中的嵌套类时,它如何引用外部类?它总是扩展外部类还是以其他方式引用它?有人告诉我,内部扩展了外部,但是为什么下面的例子不能工作呢?
例如,:
public class OuterClass {
public String fruit = "apple";
public class InnerClass {
public String fruit = "banana";
public void printFruitName(){
System.out.println(this.f
这是密码-
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public final class SetLabelForDemo {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public voi
我正尝试在微调器列表中添加几个项目。但不知何故,每次我都会在this上得到以下异常:
Cannot use this in a static context
下面是我的代码
public static void initSpinnerView(Context context, Spinner spinnerView, String prefix, int numItems, int layout) {
//spinnerView = (Spinner) findViewById(R.id.spinner2);
List<String> list = new A
在阅读时,我打开编辑器尝试一些代码示例来验证和理解它。以下是我的代码:
public enum EnumImpl {
B {
public void method() {
System.out.println(s); //(1)non-static variable s cannot be referenced from a static context
}
public static int b; //(2)Illegal static declaration in inner class
};
非公共类Out中的静态初始化块不会运行,但静态嵌套类snOut中的静态初始化块会运行。这是如何实现的,为什么呢?
public class staticNested {
static{
System.out.println("In staticNested");
}
static class sn{
static{
System.out.println("sn in staticNested");
}
void p(){
在Java语言中,嵌套类可以是static,也可以不是。如果它们是static,则它们不包含对包含实例的指针的引用(它们也不再称为内部类,它们称为嵌套类)。
在不需要引用时忘记创建嵌套类static可能会导致垃圾收集或逸出分析的问题。
有没有可能让匿名内部类也成为static?或者编译器会自动找出这一点(它可以,因为不能有任何子类)?
例如,如果我做一个匿名的比较器,我几乎不需要外部的引用:
Collections.sort(list, new Comparator<String>(){
int compare(String a, String b){
我在试着用Dataflow进行单元测试。
对于这个测试,我将从一个简单的硬编码字符串开始。
问题是,我需要将该字符串转换为pubsub消息。我有以下代码可以这样做:
// Create a PCollection from string a transform to pubsub message format
PCollection<PubsubMessage> input = p.apply("input string", Create.of("test" +
""))
下面是代码。
public class Test {
class InnerClass{
}
public static void main(String[] args){
InnerClass ic = new InnerClass();
}
}
它显示错误消息
non-static variable this cannot be referenced from a static context
after creation of object ic.
有谁能告诉我原因吗?
谢谢
我在我的应用程序中使用了room db,我使用的是MVVM设计的pattern.When,我正在创建视图模型,在访问dao方法时,它向我显示了下面的错误。
No value passed for parameter 'context'
下面是我的代码:
RoomDb.kt
@Database(entities=[UserEntity::class],version = 1)
abstract class RoomDb: RoomDatabase() {
abstract fun useDao():UserDao
companion object{
private
这个错误意味着什么,为什么它适用?我在Google上找不到太多关于成员类和静态上下文的信息,或者它们的含义,在一个似乎与我的情况相关的案例中。
下面是我得到的错误:
non-static variable this cannot be referenced from a static context
它指向此行,并指向new运算符:
throw new ParenthesisException();
ParenthesisException是main类的私有成员类。我认为这个问题可能与此有关,但这是我所能想到的全部。
这是我对ParenthesisException的定义。它在主类定义中:(如
我有两个与以下代码(链接袋)有关的问题:
public class LinkedBag<Item> implements Iterable<Item> {
private Node first; // beginning of bag
private int N; // number of elements in bag
private class Node {
private Item item;
private Node next;
}
还有这张
public class Bag<Item> implements
如果我以通常的方式声明一个类,我可以使用类名作为一个类型:
class Foo {
foo: number
constructor(x: number) {
this.foo = x
}
}
let foo : Foo // all fine
但是,如果我通过将类表达式赋值给变量来声明类,则不能将该名称用作类型:
const Bar = class Bar {
bar: string
constructor(x: string) {
this.bar = x
}
}
let b: Bar // 'Bar' refers to a valu
abstract class Person {
abstract void eat();
}
class TestAnonymousInner {
public static void main(String args[]){
Person p=new Person() {
void eat(){System.out.println("nice fruits");}
};
p.eat();
}
}
编译器生成的内部类
static class TestAnonymousInner$1 extends Pe
我试图运行完整的junit测试类包,并且我有一个域类的审计类,如下所示:
@PrePersist
public void prePersist(AuditableEntity e) {
UserService userService = SpringBeanFactory.getBean(UserService.class);
// some auditing here
}
- SpringBeanFactory类:
public class SpringBeanFactory {
private static Applica
可能重复:
public class DemoJava {
public class Hello {
void fun()
{
System.out.println("This is static fun man!!");
}
}
public static void main(String[] args) {
Hello hello = new Hello();
hello.fun();
}
}
在本例中,它将给出一
我知道如何创建内部类的实例。但我想知道为什么我们不能这样做:
class outerclass{
public static void main(String[] args){
innerclass in=new innerclass();
}
class innerclass{
}
}
如果我这样做,我会得到以下错误:
No enclosing instance of type outerclass is accessible. Must qualify the allocation with an enc
当我将项目作为public class HomeActivity extends Activity implements OnClickListener使用,在项目内部使用getContentResolver时,它会显示错误
The method getContentResolver() is undefined for the type new View.OnClickListener(){}
但是当我使用public class HomeActivity extends ActionBarActivity时,它对我很有用,但是我如何在public class HomeActivity ex