Java program के लिए हमेशा class name के जैसे ही file name भी रखते हैं। means जो class का name होगा वही file का name भी होगा। और file extension .java के साथ save करते हैं।

Java Program Structure

programming language C , C++ की तरह ही java program का entry point main() method होता है। जैसा कि नीचे example में आप देख सकते हैं।

File : Test.java

CopyFullscreenClose FullscreenRun
public class Test {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

Explain :

  1. तो जैसा कि आपने ऊपर पढ़ा कि java program का entry point main() method होता है , हम जो implementation चाहते हैं उसके लिए पूरा code main() method में ही लिखते हैं।

  2. class name के first letter को हमेशा uppercase में लिखते हैं। और एक से ज्यादा words होने पर उसे camel case में लिखते हैं जैसे : UserAddress

  3. ध्यान रहे कि Java case-sensitive language है , तो Test और test दोनों अलग अलग हैं।

  4. Program की class name और file name same होना चाहिए।

Java main Method

main() method को आप हर जावा program में देखेंगे। main() method के अंदर जो भी code होगा वो execute हो जयगा जब भी program run होगा।

public static void main(String[] args)

method को आगे जो keywords use किये गए हैं , उन्हें आगे deeply अच्छे से पढ़ेंगे।

Java System.out.println()

System एक predefined class है जिसमे कई useful members और properties हैं जैसे out . इसकी full form output है। और println() एक method है जिसका use किसी भी text को print करने के लिए किया जाता है। इसकी full form print line है।

Important

Normally हम file name और class name same रखते हैं ,लेकिन ऐसा नहीं कि आप file का नाम class name से different रख सकते हैं। Actually java program को compile करने के लिए file name का use किया जाता है , जबकि complied code को execute कराने के लिए file में present main class का name use किया जाता है।
See Example -

file : Test.java

CopyFullscreenClose FullscreenRun
class A 
{ 
   public static void main(String args[])
   { 
      System.out.println("Class A running"); 
   } 
} 
Output
javac Test.java
java A
Class A running

Example में आप देख सकते हैं कि , code को compile करने के लिए filename use किया गया है और compiled code execute करने के लिए class name use किया गया है।

Important
ध्यान रहे कि अगर file का नाम class के name से different है तो वह class public नहीं होनी चाहिए नहीं तो compiler error देगा।

public class A {}

javac Test.java

class A is public, should be declared in a file named Test.java
public class A {
       ^
1 error

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers