glowing713
Frontend-Deep-Dive
glowing713
전체 방문자
오늘
어제
  • 분류 전체보기 (97)
    • Languages (11)
      • JavaScript 💛 (3)
      • Python 🐍 (4)
      • Java ☕️ (3)
      • Swift 🧡 (1)
    • Computer_Science (1)
      • Computer_Network 🕸 (1)
    • Web_Frontend (4)
      • Vue.js (1)
    • Problem_Solving (76)
    • Server (1)
      • Spring 🍀 (1)
    • AI (2)
      • NLP 🗣 (1)
      • AI_Math ➗ (1)
    • 개발환경 꾸미기 ✌ (1)
    • 생각정리 ✍🏻 (1)

블로그 메뉴

  • 🧑🏻‍💻Github

공지사항

인기 글

태그

  • c++
  • brute-force
  • 카카오 기출
  • Baekjoon
  • DP
  • boostcampaitech
  • 2019 카카오 개발자 겨울 인턴십
  • 동적계획법
  • Algorithm
  • 이분탐색
  • BOJ
  • bfs
  • 완전탐색
  • Stack
  • 프로그래머스
  • Java
  • ps
  • mst
  • Python
  • binary search

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
glowing713

Frontend-Deep-Dive

You can not write Java code without defining a class.
Languages/Java ☕️

You can not write Java code without defining a class.

2021. 1. 4. 00:58

좀 뜬금없지만, 클래스 없이 자바를 쓸 수 있을까...? 🤔

Yes, you need at least one class to have a program, but no, you do not need any methods (contrary to some other answers).

The reason you need a class is because in Java, all code is inside classes. So to have any code, you need a class. However, code doesn't necessarily need to be in a method. It can also be in initializers. So, here is a complete Java program with no methods:

class LookMaNoMethods {
    static {
        System.out.println("Hello, world!");
        System.exit(0);
    }
}
Hello, world!

(출처: https://stackoverflow.com/questions/5204385/does-every-program-in-java-require-a-class)

저 글이 2011년 글인데, 위의 소스코드처럼 작성하면 실행할 수 있었다. 그러나 Java 7부터 main 함수는 필수가 되었다.
지금 저렇게 코드를 작성하고 실행하면 메인 함수를 정의하라는 에러메시지가 발생한다.

Error: Main method not found in class StudyMain, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

한마디로, 답은 NO 이다.

Java의 모든 코드는 클래스 내부에 작성되어야 한다.

저작자표시 비영리 변경금지 (새창열림)

'Languages > Java ☕️' 카테고리의 다른 글

[java-live-study] 🌈 5주차 과제: 클래스  (0) 2021.01.04
[java-live-study] 🔥 4주차 과제: 제어문  (0) 2021.01.04
    'Languages/Java ☕️' 카테고리의 다른 글
    • [java-live-study] 🌈 5주차 과제: 클래스
    • [java-live-study] 🔥 4주차 과제: 제어문
    glowing713
    glowing713

    티스토리툴바