This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a local instance of a class

 
Jack Weavesham
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to call a method in a foreign class (eg ClassA). How do I create an instance of this class?
 
Tom Purl
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on whether the method is a static or instance method. You might want to check out Sun's java tutorial first to refine your question a little bit.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Weavesham:
I want to call a method in a foreign class (eg ClassA). How do I create an instance of this class?

Typically you create instances of a class by calling its constructor using the new operator:

There are situations, though, where you are not allowed to do this (that is, there is no public constructor to call), so you have to call another method to get an instance of the class.
Did that help?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple example of creating an instance of a class and invoking an instance method:Sun's basic J2SE tutorial is located at http://java.sun.com/docs/books/tutorial/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic