inblog logo
|
An's Blog
    트러블 슈팅

    [트러블 슈팅] 2. 클래스를 불러오지 못함

    윤설안's avatar
    윤설안
    Jun 10, 2025
    [트러블 슈팅] 2. 클래스를 불러오지 못함
    Contents
    문제의 코드결과해결이유

    문제의 코드

    package ex01; class Person { String name = "홍길동"; int age = 14; } // Dog(강아지) = 이름(name), 색깔(color), 좋아하는 간식들 (foods) = "개 껌", "개 뼈다귀" class Dog { String name = "나비"; String color = "하늘색"; String[] foods = {"개 껌", "개 뼈다귀"}; } // 문자열(이름), 숫자(나이) 함께 저장 public class Var05 { public static void main(String[] args) { System.out.println(Dog.name); System.out.println(Dog.color); System.out.println(Dog.foods[0]); System.out.println(Dog.foods[1]); } }

    결과

    notion image
    번역 : java: 정적 컨텍스트에서 비정적 변수 이름을 참조할 수 없습니다

    해결

    package ex01; class Person { static String name = "홍길동"; static int age = 14; } // Dog(강아지) = 이름(name), 색깔(color), 좋아하는 간식들 (foods) = "개 껌", "개 뼈다귀" class Dog { static String name = "나비"; static String color = "하늘색"; static String[] foods = {"개 껌", "개 뼈다귀"}; } // 문자열(이름), 숫자(나이) 함께 저장 public class Var05 { public static void main(String[] args) { System.out.println(Dog.name); System.out.println(Dog.color); System.out.println(Dog.foods[0]); System.out.println(Dog.foods[1]); } }

    이유

    클래스 안에 변수 앞에 static을 붙이지 않아서 메인에서 클래스를 불러오지 못 함.
     
    Share article
    Contents
    문제의 코드결과해결이유

    An's Blog

    RSS·Powered by Inblog