Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

xaadi1993

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

  • one year ago
  • one year ago

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

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

    • one year ago
  2. dpflan
    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.

    • one year ago
  3. xaadi1993
    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 ...

    • one year ago
  4. dpflan
    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.

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

    hmmmmm

    • one year ago
  6. dpflan
    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); } }

    • one year ago
  7. dpflan
    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(); } }

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

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

    • one year ago
  9. dpflan
    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.

    • one year ago
  10. dpflan
    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.

    • one year ago
    • Attachments:

See more questions >>>

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.