Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Calling another class

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you instantiate a class that has a main method and a constructor, what happens? I've tried this and both the constructor and the main() method seem to be getting ignored - and no error occurs.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if you run>java Test
the main method is executed which creates an object of type Test which executes the constructor. The output is:
In main
In constructor
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
since the main method is static, you do not instantiate an object by just calling it. only at the point where you call the constructor a object is created.
pascal
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmm. But can you call the main of the second class as member method of that class? Like this.
<pre>
class A
{
public A()
{
System.out.println("in constructor");
}
public static void main(String args[])
{
System.out.println("in class A in main");
}
}
class Test
{
public static void main(String args[])
{
A aclass = new A();
aclass.main(args);
}
}</pre>
-Jason

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is :
What happens here when you execute class A? When I do this, nothing happens.

public class A
{
public static void main (String args[])
{
new B();
}
}
public class B
{
public B()
{
System.out.println("I'm in B constructor");
}
public static void main (String args[])
{
System.out.println("I'm in B.main()");
}
}
BTW, how do I get code to display in that small print? - the way everybody else does it.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the [ code] and [ /code] tags (without the spaces) and it will do that. If you just click the pad and pencil on my post above you can see how I did it. You won't be able to save any edits, but you can look at the typed post.
Your sample should print the "I'm in B constructor" message to the console.
Lots of classes have main methods that are never used to start an application. They are usually there for self testing purposes, and don't really get in the way if the class is not used to start an application.
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used the HTML tags pre and /pre before and after my code respecitvely. Of course, you will want to enclose the tags in <>.
So, I guess you can use the main method as I described? Pauls, your code should display as Cindy mentioned. It is interesting trivia if nothing else.
-Jason
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
Lots of classes have main methods that are never used to start an application. They are usually there for self testing purposes, and don't really get in the way if the class is not used to start an application.


Yes, for example, I usually have a boolean final DEBUG property used to test the single classes:


------------------
Fabrizio Gianneschi
Sun Certified Programmer for Java2 Platform
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
main is an entry point into an application. If you create the class from another class, then main will never be called. The main method is only called when you start that class as an entry point (e.g. java ClassA). Constructors are used to instantiate and create objects.
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic