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
반응형