Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Cracked.

 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
This thread is for discussing about the abnormal java.

Java states, we can do things in more than one way !.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some one told that one class must contain main method to start executing that class...but that is not the case....
see here

class Starter{
...
pubilc void doit(){ .. . }
. .
}

public class Start_It{
static{
(new Starter()).doit();
}

will work without Main() Method...but only drawback is it is unable to get the environment variables.......


any one know other methods to start a class executing without using the Main() method in that class......

if so please post it...
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how do you propose to run class Start_It? Maybe with a main method

In other contexts you don't need a main method, e.g. applets or servlets, but they have other standardized methods of invocation.
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by j rangavallika:

Java states, we can do things in more than one way !.


I believe that is the Perl motto, There's more than one way to do it (TMTOWTDI).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can start a "program" like this with the normal Java launcher; it loads the class in order to look for main(), and loading it will cause this code to run. If this initializer ever returns, though, you'll see the error message about not being able to find main!

That said, I should mention that this old chestnut has been known forever; every book that purports to teach you "advanced Java" trots it out and dusts it off.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
You can start a "program" like this with the normal Java launcher; it loads the class in order to look for main(), and loading it will cause this code to run. If this initializer ever returns, though, you'll see the error message about not being able to find main!

That said, I should mention that this old chestnut has been known forever; every book that purports to teach you "advanced Java" trots it out and dusts it off.



I don't think so........ there must be some other way the java virtual machine is doing in that....and i think static will not return anything....
can you explain with an example...please......
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the next quest......


interface a{
....
....
}

interface b extends a {
....
...
}

now can i connect any other two classes by using these two interfaces.....
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WTF?
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by j rangavallika:

I don't think so........ there must be some other way the java virtual machine is doing in that....and i think static will not return anything....
can you explain with an example...please......


This code:

is a static initalizer block and is executed when the class is loaded (see here for more explanation). As Ernest points out, one could issue the command:

and the JVM would load the class to look for the main() method. The static initalizer is run and it invokes Starter. There is nothing to return because the initalizer is not a method. This is not "advanced" Java. It is obfuscated Java.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by j rangavallika:
here is the next quest......


interface a{
....
....
}

interface b extends a {
....
...
}

now can i connect any other two classes by using these two interfaces.....


What do you mean by "connect any other two classes"?

This sounds an awfully like a homework assignement and doesn't even seem like "advanced" Java. If you explain what you want to do and show us what you have tried already, we can help from there.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:

is a static initalizer block and is executed when the class is loaded (see here for more explanation). As Ernest points out, one could issue the command:

and the JVM would load the class to look for the main() method. The static initalizer is run and it invokes Starter. There is nothing to return because the initalizer is not a method. This is not "advanced" Java. It is obfuscated Java.


Is there an echo in here?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
doesn't even seem like "advanced" Java.


Agreed. Moving to intermediate...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic