If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Java program के लिए हमेशा class name के जैसे ही file name भी रखते हैं। means जो class का name होगा वही file का name भी होगा। और file extension .java के साथ save करते हैं।
programming language C , C++ की तरह ही java program का entry point main() method होता है। जैसा कि नीचे example में आप देख सकते हैं।
File : Test.java
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
तो जैसा कि आपने ऊपर पढ़ा कि java program का entry point main() method होता है , हम जो implementation चाहते हैं उसके लिए पूरा code main() method में ही लिखते हैं।
class name के first letter को हमेशा uppercase में लिखते हैं। और एक से ज्यादा words होने पर उसे camel case में लिखते हैं जैसे : UserAddress
ध्यान रहे कि Java case-sensitive language है , तो Test और test दोनों अलग अलग हैं।
Program की class name और file name same होना चाहिए।
main() method को आप हर जावा program में देखेंगे। main() method के अंदर जो भी code होगा वो execute हो जयगा जब भी program run होगा।
public static void main(String[] args)
method को आगे जो keywords use किये गए हैं , उन्हें आगे deeply अच्छे से पढ़ेंगे।
System एक predefined class है जिसमे कई useful members और properties हैं जैसे out . इसकी full form output है। और println() एक method है जिसका use किसी भी text को print करने के लिए किया जाता है। इसकी full form print line है।
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
class A
{
public static void main(String args[])
{
System.out.println("Class A running");
}
}
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
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook