• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling other Classes from main class  RSS feed

 
Josh Williams
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found many examples of creating classes, but how do you call the classes into you code? Example one main class that references multiple classes to build it. I am new to Java and OO. I have done some programming in C++ and Visual Basic.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Williams,



I have found many examples of creating classes, but how do you call the classes into you code? Example one main class that references multiple classes to build it. I am new to Java and OO. I have done some programming in C++ and Visual Basic



Calling classes in the sense, creating instances of the classes. You need to import the class and create the instances.

Like :


You need not to import java.lang.String and java.lang.Integer because java.lang is automatically imported to your app.

You need to do some programming in Java. Things will be more clear then.


Thanks and Regards,
cmbhatt
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A very very simple example:
Say you create a simple class called appropriately "SimpleClass" that has one class variable, a string called "myString", and has one method, "callThisClass()", and has one constructor that allows you to initialize the string. You would create the class like so:


To use this class in a separate class you first declare an object of the class:

Before you can use this object however you must construct it. This allocates memory for the object, its variables and its methods.

Now you can use the object:

You can also compress the declaration and instantiation into one step:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!