我是Java新手,我的代码有些问题。
可以使用非静态方法displayText.setText from JTextArea类在我的静态类中显示。
非常感谢您的任何帮助!
这是我的密码:
public class GUI extends javax.swing.JFrame {
Thread send, receive, abc;
public String x;
private String response, nonStaticString;
public GUI() {
initComponents();
abc = ne
据我所知,非静态方法将为其类obj &所有封闭类分配"this“变量。
public class TestNested {
int a=4;
public static class a{
int a=5;
static int c=10;
class b{
int a=6;
void aaa(){
int a=7;
TestNested t=new TestNested();
System.out.println(this.a);
请原谅我的无知。我是个初学者:
为什么下面的代码会给我下面的编译错误?第16行:不能从静态上下文引用非静态变量x
public class average{
int [] numbers = {2,3,4,5,6};
double x = averageMark(numbers);
public static double averageMark(int [] numbers){
int sum = 0;
double average = 0.000;
for (int i = 0; i < numbers.length; i++){
sum
这可能是一个愚蠢的问题,但它已经让我发疯好几天了。首先,我说的是嵌入到更大的应用程序中的代码,所以类和方法签名是强加的。
因此,我的目标是创建一个GC信息集合,代码如下:
public final class JVMMemStats_SVC {
public static final void JVMMemStats(IData pipeline) throws ServiceException {
List<GarbageCollectorMXBean> gcMBeans = ManagementFactory.getGarbageCollectorMXB
我无法编译下面的代码,因为我有17个关于“非静态变量-这不能从静态上下文中引用”的错误。它总是指向this关键字。
package MyLib;
import java.util.*;
class Book {
static int pages;
static String Title;
static String Author;
static int status;
static String BorrowedBy;
static Date ReturnDate;
static Date DueDate;
public static final int
BORROWED = 0
他说:我有一个像这样的程序。
public class Test{
void dispose(){
System.out.println("disposing");
}
Test t=new Test();
public static void main(String[] args){
t.dispose();
}
}
为什么我不能从main()调用dispose方法?如果它是静态和非静态关系,为什么下面的代码可以工作?
public class Test{
void dispose(){
在阅读时,我打开编辑器尝试一些代码示例来验证和理解它。以下是我的代码:
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
};
我有三个类:-
Tell -主程序
项目-单独的电话目录项目
目录-存储所有项的目录对象。
我要做的是在目录中创建一个数组列表,它存储来自item类的对象,我就是这样做的。
从Tell,我调用的方法如下:-
Directory.add(name, telNo);
目录类:-
public class Directory
{
ArrayList<Entry> entries = new ArrayList<Entry>();
// Add an entry to theDirectory
public static void add(St
我创建了一个非静态内部类,如下所示:
class Sample {
public void sam() {
System.out.println("hi");
}
}
我在main方法中像这样调用它:
Sample obj = new Sample();
obj.sam();
它给出了一个编译错误:当我将非静态内部类声明为静态时,它可以工作:non-static cannot be referenced from a static context。为什么会这样呢?
这是我刚刚写的代码的一部分。基本上,类Document实现了Iterable接口。迭代器将像链表一样遍历节点。在remove方法中,我使用了Document类范围内的nodeMap引用。但是,this引用应该引用Iterator本身,那么它怎么能找到那个对象呢?或者Iterator是Document的子类
我以前没有想过这个问题。突然间就把自己搞糊涂了。
public class Document implements Iterable<DocumentNode> {
Map<Integer, DocumentNode> nodeMap;
public
我在编写一个简单的程序时遇到了一个问题。两天前我开始使用Swing库,所以我还不够好。错误是:“非静态变量--这不能从静态上下文中引用”,引用为"WindowsEraser listener =();“。有什么问题吗?
public class prog9{
public class WindowEraser extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public static v
我需要从静态类中的方法引用顶级类的变量。
此方法应该作用于顶级类的唯一实例,因此感觉我不应该在静态类中实例化顶级类。
基本上我想要像这样的东西
public class TopLevel{
// private
int innerV
public static class Inner implements X {
for(i=0; i<innerV,i++){
doSomething
}
}
}
在for循环和类似的地方,是否可以只说this.innerV或类似的东西?
我真的很困惑!我有两个班,俱乐部和会员。在会员方面,我有一个方法,getMonth(),在Club I中有一个joinedMonth(),它接受参数' month‘,所以用户输入一个月,然后我希望它返回在那个特定月份中加入的成员资格。
我试图从class Club调用getMonth()方法,这样我就可以继续比较月份的整数了。但是,当我试图调用该方法时,我只得到了前面提到的“不能从静态上下文引用非静态方法getMonth()”。
基本上,这是什么,我如何解决它?
提前谢谢你!
俱乐部:
public class Club
{
private ArrayList<Membe
class Test
{
private $flag;
public function __construct($flag)
{
$this->flag = $flag;
}
public function a()
{
if ($this->flag)
{
$this->b();
}
}
public function b()
{
$this->c();
}
public
当我用main method.Can为类创建对象时,我可以在main方法中使用这些实例变量,因为它们在同一个类中。
class matrix
{
int i,j;
int a[10];
Scanner one=new Scanner(System.in);
public static void main(String args[])
{
matrix obj=new matrix();
System.out.println("Enter the numbers");
obj.create(a); // is it correct
Netbeans告诉我,从非静态方法访问静态方法是不好的。为什么这很糟糕?"Accessing method getInstance“是一个警告:
import java.util.Calendar;
public class Clock
{
// Instance fields
private Calendar time;
/**
* Constructor. Starts the clock at the current operating system time
*/
public Clock()
{
我需要创建一个具有从8到12的随机值的数组,但它显示我的变量不兼容。我有什么需要改变的?X不应该是int类型的吗?
以下是包含该问题的代码的第一部分:
public class Fish {
int min = 8;
int max = 12;
int x = min + (int)(Math.random() * ((max-min) + 1));
static Fish[] myFish = new Fish[x];
static int Fcount=0;
private float weight;
public Fish(float w) { weight = w;
myFi
我有一个类,它有另一个static内部类:
class A {
private List<String> list;
public static class B {
// I want to update list here without making list as static
// I don't have an object for outer class
}
}
我想知道非静态方法如何修改静态变量。我知道静态方法只能访问其他静态方法和静态变量。然而,另一面是真的吗?非静态方法只能访问非静态变量吗?例如:
public class SampleClass {
private static int currentCount = 0;
public SampleClass() {
currentCount++;
}
public void increaseCount() {
currentCount++;
}
}
这段代码是编译的,我想知道为什么会有静态访问权限。
所以我有outerClass和innerClass,我想使用outerClass访问从innerClass创建的对象,例如:
public Class outerClass{
Class innerClass{
//properties
}
}
所以我想做的是这样的:
public class Main {
public static void main(String [] args) {
outerClass outerObj = new outerClass();
outerClass.innerClass inner