greenhelix
greenhelix
greenhelix
07-09 20:34
  • All (229)
    • Algorithm (118)
      • Algorithm (17)
      • Graph (0)
      • Core (6)
      • Python (18)
      • PythonSnippet (4)
      • Java (59)
      • Kotlin (14)
    • Project (0)
    • Study (8)
      • License (5)
      • EIP (3)
    • Programming (63)
      • Android (41)
      • Flutter (1)
      • Bugs Life (21)
      • Linux (0)
    • Tech (32)
      • Tech (17)
      • Drone (4)
      • Hacking (11)
    • Life (6)
      • INGRESS (1)
      • 심시티빌드잇 (0)
250x250

티스토리

hELLO · Designed By 정상우.
greenhelix

greenhelix

[VS] Cannot make a static reference to the non-static method ..... from the type ... Java(603979977)
Programming/Bugs Life

[VS] Cannot make a static reference to the non-static method ..... from the type ... Java(603979977)

2020. 8. 13. 12:18

Cannot make a static reference to the non-static method ㅁㅁㅁ from the type ㅁㅁㅁJava(603979977)

static 으로 동일 하게 맞춰주면된다. 

코드로 보면 이해된다. 

public class Compressed {

    public static void main(String[] args) {
        System.out.println("Hello");
        String show = "abbccccc";
        System.out.println(compress(show));
    }

    String compress(String str) {
        StringBuilder compressed = new StringBuilder();
        int countConsecutive = 0;
        for (int i = 0; i < str.length(); i++) {
            countConsecutive++;
            if (i + 1 >= str.length() || str.charAt(i) != str.charAt(i + 1)) {
                compressed.append(str.charAt(i));
                compressed.append(countConsecutive);
                countConsecutive = 0;
            }
        }
        return compressed.length() < str.length() ? compressed.toString() : str;
    }
}

아래와 같이 Static을 추가해주면 된다... 간단하다.

public class Compressed {

    public static void main(String[] args) {
        System.out.println("Hello");
        String show = "abbccccc";
        System.out.println(compress(show));
    }

    static String compress(String str) {
        StringBuilder compressed = new StringBuilder();
        int countConsecutive = 0;
        for (int i = 0; i < str.length(); i++) {
            countConsecutive++;
            if (i + 1 >= str.length() || str.charAt(i) != str.charAt(i + 1)) {
                compressed.append(str.charAt(i));
                compressed.append(countConsecutive);
                countConsecutive = 0;
            }
        }
        return compressed.length() < str.length() ? compressed.toString() : str;
    }
}

참고 블로그 

 

Cannot make a static reference to the non-static field 오류

자바로 코딩하던 중 아래와 같은 코드에서 오류에 직면했다. 자바는 보다 완벽한 객체지향 프로그래밍을 위해 생성된 언어이며, Class를 선언함으로써 객체지향을 달성한다는 사실을 잊고 있어�

wookoa.tistory.com

 

728x90
반응형
저작자표시 비영리 변경금지 (새창열림)

'Programming > Bugs Life' 카테고리의 다른 글

[GitHub] a lock file already exists in the repository, which blocks this operation from completing.  (3) 2020.08.27
[Flutter] :: Error#04 => 안드로이드 스튜디오 업데이트 후 플러그인이 업데이트가 안된다?  (0) 2020.06.12
[Flutter] :: Error#03 => 폴더 이름이 왜 다 빨간색?  (0) 2020.05.16
    'Programming/Bugs Life' 카테고리의 다른 글
    • [Android] Button 위치 에러 :: [This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints.]
    • [GitHub] a lock file already exists in the repository, which blocks this operation from completing.
    • [Flutter] :: Error#04 => 안드로이드 스튜디오 업데이트 후 플러그인이 업데이트가 안된다?
    • [Flutter] :: Error#03 => 폴더 이름이 왜 다 빨간색?
    greenhelix
    greenhelix
    개발에 관한 것들과 개인적인 것을 담는 블로그

    티스토리툴바