Quantcast

A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

xaadi1993

  • 2 years ago

Why is so that a program in JAVA also runs with static block only without any main ??

  • This Question is Closed
  1. dpflan
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    You want to know if you always need the main() method?

  2. dpflan
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Because Java is Object-Oriented, everything is an object. Static methods allow you to use a block of code without instantiating an instance of that class.

  3. xaadi1993
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    public class WithoutMain { static { System.out.println("Program without Main"); System.exit(0); } } why this dont need any main method as the only way to run without main is making applet ...

  4. dpflan
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    All right, this is a interesting question. So, labeling something as static within a class makes that item part of the class itself and not the object. For instance, if you were to give a static variable 'count' which you use to keep track of the number of cars you've created, that value would sort of exist outside of each Car object created.

  5. xaadi1993
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    hmmmmm

  6. dpflan
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    For your static block, if you were to instantiate more than one 'WithoutMain' class object, you would only see the code in the static block printed once. public class Test { public Test() { } static { System.out.println("Program without Main"); System.exit(0); } }

  7. dpflan
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    public class TestTwo { static { Test test1 = new Test(); Test test2 = new Test(); Test test3 = new Test(); } }

  8. dpflan
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    output after execution is: ~/D/javaTest> java TestTwo Program without Main

  9. dpflan
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    So this does then depend upon the environment in which you are using you Java classes. An applet is made so that it can be used on the web, while non-applets are not.

  10. dpflan
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Thanks, but I feel I may not have given the real answer. I guess this actually is difficult question, I've seen mention that since Java is Object-Oriented that main method is an 'entry point' into a class.

  11. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.