The Artima Developer Community
Sponsored Link

Java Answers Forum
How to call classes

2 replies on 1 page. Most recent reply: Mar 28, 2003 5:45 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Mira

Posts: 3
Nickname: mizza
Registered: Mar, 2003

How to call classes Posted: Mar 28, 2003 3:54 AM
Reply to this message Reply
Advertisement
I need 3 classses in 1 file and i need to call the second and third classes from the Main class.
What do i have to do to the other classes to make this possible (while keeping the file in essentially this layout)?

public class Main{

//need to call classes here

}

public class Secondary{

public DoStuff(){
//stuff
}
}

public class ThirdClass{

public DoThings(){
//things
}
}


Dhrubo

Posts: 32
Nickname: dhrubo
Registered: Mar, 2003

Re: How to call classes Posted: Mar 28, 2003 6:19 AM
Reply to this message Reply
Dear Mira, Your program is wrong because you cannot have more than 1 public class declaration in a single class file.

Please come up with more details so that i can help you

Regards
Dhrubo
(dhrubo@zeenext.com)

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How to call classes Posted: Mar 28, 2003 5:45 PM
Reply to this message Reply
You can't call classes, you can only call methods.

You have a just few things to fix:
- Most importantly, your opening curlies were in the wrong place. They should be on the next line, not the same line as the condition or declaration.
- You need to instantiate instances (objects) of a class and use those.
- Only one class in a file can be public.
- You need to use the [java] tags when posting code (see Formatting Your Post box on the right when posting).
- Your naming convention looks like C#, not Java; in Java, your method names start with a lowercase letter.

/**
 * Incredible stuff-doing technology.
 */
public class Firstdary
{
   public String dontHardlyDoNuttin()
   {
      return getClass().getName() + " didn't hardly do nuttin'.";
   }
 
   public static void main( String [] args )
   {
      Firstdary firstDairy = new Firstdary();
      Secondary secondDairy = new Secondary();
      Thirdary  thirdDairy = new Thirdary();
 
      String stuff = firstDairy.dontHardlyDoNuttin() + "\n" + 
                     secondDairy.doStuff() + "\n" + 
                     thirdDairy.doThings();
 
      System.out.println( "Results of this incredible technology:\n" + stuff );
   }
}
 
/**
 * This is the class to use if you want to get stuff done.
 */
class Secondary
{
   public String doStuff()
   {
      return getClass().getName() + " did some stuff!";
   }
}
 
/**
 * This is the class to use if you want to get things done.
 */
class Thirdary
{   
   public String doThings()
   {
      return getClass().getName() + " did some things!";
   }
}

Flat View: This topic has 2 replies on 1 page
Topic: how can i read individual String  from a String Previous Topic   Next Topic Topic: applet and proxy

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use