anonymous
  • anonymous
Why is so that a program in JAVA also runs with static block only without any main ??
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
You want to know if you always need the main() method?
anonymous
  • anonymous
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.
anonymous
  • anonymous
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 ...

Looking for something else?

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

More answers

anonymous
  • anonymous
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.
anonymous
  • anonymous
hmmmmm
anonymous
  • anonymous
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); } }
anonymous
  • anonymous
public class TestTwo { static { Test test1 = new Test(); Test test2 = new Test(); Test test3 = new Test(); } }
anonymous
  • anonymous
output after execution is: ~/D/javaTest> java TestTwo Program without Main
anonymous
  • anonymous
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.
anonymous
  • anonymous
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.

Looking for something else?

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