1

I am a beginner in Java. I have two packages in my current project. Each of the packages have two classes called the "packageClassOne" and "packageClassTwo".

The packageClassTwo has a constructor and other public methods.

I want to call the PackageClassTwo from an if statment located in the PackageClassOne. My code looks something like this

packageClassOne:

 public class packageClassOne {
    public void selectComponent(boolen) {
       if(/* check condition*) {
          packageClassTwo value = new packageClassTwo();
       }
    }
 }

packageClassTwo:

 public class packageClassTwo {

    public packageClassTwo(String name){     //Constructor
         int length = name.length();
     }

   public String CreateWord(){
      /*functionality ofthis method*/
   }

    public String CreateSentence(){
      /*functionality ofthis method*/
   }
 }  

The problem is that everytime I call the packageClassTwo from my packageClassOne it tries to call the constructor instead of calling the class itself. I want to call the entire packageClassTwo instead of just the constructor.

Can somebody help me please? Thank you in advance for your help

Goldengirl
  • 957
  • 4
  • 10
  • 30

4 Answers4

5

Since Java is an object oriented language, you have to have a mindset of dealing with instances that are realizations of the classes you defined. These are the objects.

So if you want to call a method from packageClassTwo class, you first create an object of packageClassTwo. You seem to be trying to do just this. Once you have the object, you can call its methods. For example

//Instantiate an object by calling the constructor
packageClassTwo object = new packageClassTwo(string);
//Now call its methods
String val = object.CreateWord()

There is no such thing as "calling a class". You call methods of objects of a class.

Occasionally, there might be a well founded need to call methods of a class without initializing objects. Look into static methods and classes for further reading.

Community
  • 1
  • 1
milez
  • 2,201
  • 12
  • 31
2

If you want to call all methods of packageClassTwo you have to do it explicitly

packageClassTwo pct = new packageClassTwo("");
pct.CreateWord();
pct.CreateSentence();

If you allways want the 2 methods to be called when you create a new packageClassTwo object, than you can just add the calls to the constructor

public packageClassTwo(String name) {     
     int length = name.length();
     pct.CreateWord();
     pct.CreateSentence();
 }

Edit:

Note that in the second case, if you end up only calling the 2 methods from inside the constructor, it is better to make them private.

As a sidenote, it is a general convention in java to have class names start with a upper case letter : PackageClassTwo not packageClassTwo, and method names to start with lower case createWord not CreateWord. This wll make your code more readable.

noscreenname
  • 3,314
  • 22
  • 30
0

If you want to call all the methods from the packageClassTwo, call them from the packageClassTwo constructor

 public packageClassTwo(String name)
 {    
     int length = name.length();
     CreateWorld();
     CreateSentence();
 }
gygabyte
  • 176
  • 2
  • 12
0

I don't think your code will run without compiling errors.because you did not declare the constructor packageClassTwo().

wero
  • 3
  • 2
  • A default no argument constructor will be added at the compile time. – noscreenname Jul 22 '16 at 09:15
  • No.in fact, your IDE will show you red error at once, if you try to new instance with default constructor. – wero Jul 22 '16 at 09:21
  • the default constructor only be added if you didn't define any constructor. – wero Jul 22 '16 at 09:22
  • You are right, the default constructor wil be added to packageClassOne and not packageClassTwo. I missread your answer. On the other hand, packageClassTwo has a constructor with String parameter, so your answer is still incorrent. – noscreenname Jul 22 '16 at 09:25