Hi all, I hope you are well. I am trying to explain some code to a friend :

StringConcatenate StringConcatenate1 = new StringConcatenate(); 
StringConcatenate1.stringConcat();

I said that this code creates an object "StringConcatenate1 " of type StringConcatenate and executes method stringConcat() (which is from class StringConcatenate()) on object StringConcatenate1. Is this correct ? What I am trying to do here is call another class from within a different class (all in same package). Been a while since I have done Java so please excuse the simple question lol. I also said to him that we can just call the method from that class using a class reference and without creating an object like so :

StringConcatenate.stringConcat();

Is my thinking correct here? And is there a more elegant way to call a method in another class? Oh, and is there a convention for naming objects and is StringConcatenate1 an acceptable name?

Hi all, I hope you are well. I am trying to explain some code to a friend :

StringConcatenate StringConcatenate1 = new StringConcatenate(); 
StringConcatenate1.stringConcat();

I said that this code creates an object "StringConcatenate1 " of type StringConcatenate and executes method stringConcat() (which is from class StringConcatenate()) on object StringConcatenate1. Is this correct ? What I am trying to do here is call another class from within a different class (all in same package). Been a while since I have done Java so please excuse the simple question lol. I also said to him that we can just call the method from that class using a class reference and without creating an object like so :

StringConcatenate.stringConcat();

Is my thinking correct here? And is there a more elegant way to call a method in another class? Oh, and is there a convention for naming objects and is StringConcatenate1 an acceptable name?

http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html StringConcatenate1 is not a good name for the object. stringConcatenate1 would be better. See the link on conventions. Whether something is in the same package is relevant to the public/private/protected nature of the class and the methods and where you import it from, but I can't think of a reason why it would affect how you called it. See this link for private/public/protected modifiers and packages, subclasses, etc.

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

StringConcatenate.stringConcat();

This is fine assuming that stringConcat () is a static method within the StringConcatenate class. If it isn't a static method, you'll need to call it with an object.

Overall, your explanations seem fine to me. Just add the part about the static modifier. I believe you'll get an error for this:

StringConcatenate.stringConcat();

if it's not a static method, and you'll get a warning (but not an error) for this:

StringConcatenate1.stringConcat();

if it IS a static method.

commented: Excellent response, thank you! +4

Classes begin with capital. Methods with lower.

StringConcatenate stringConcatenate1 = new StringConcatenate(); 
stringConcatenate1.stringConcat();

You create an object/instance of type StringConcatenate. And you call the method stringConcat() of the instance stringConcatenate1.

Most of the time different instances have different results when they call the same method:

StringConcatenate string1 = new StringConcatenate("aaa"); 
StringConcatenate string2 = new StringConcatenate("bbb");

string1.stringConcat();  
string2.stringConcat();

That is why we create different instances of the same class.

Also this would only be correct if the method was declared "static"

StringConcatenate.stringConcat();

If the method is static you don't need to create an instance to call it

commented: Excellent response, thank you! +4

Excellent, thanks very much, both of you. Quick answers there too!

Hi all, I hope you are well. I am trying to explain some code to a friend :

StringConcatenate StringConcatenate1 = new StringConcatenate(); 
StringConcatenate1.stringConcat();

I said that this code creates an object "StringConcatenate1 " of type StringConcatenate and executes method stringConcat() (which is from class StringConcatenate()) on object StringConcatenate1. Is this correct ? What I am trying to do here is call another class from within a different class (all in same package). Been a while since I have done Java so please excuse the simple question lol. I also said to him that we can just call the method from that class using a class reference and without creating an object like so :

StringConcatenate.stringConcat();

Is my thinking correct here? And is there a more elegant way to call a method in another class? Oh, and is there a convention for naming objects and is StringConcatenate1 an acceptable name?

Your first point is right. You can very well create a reference and invoke the method of the class.

Your second point that "we can just call the method from that class without creating an object" is valid only when the method you're invoking is static.

In java there's convention for naming Classes and variable.
Name of the class starts with capital letter. So you're right in naming Class name.

Variable name follows CamelCase or camelCase nomenclature.
But I personally like to keep the first letter of the variable name small.
So I'd have written something like this....

StringConcatenate stringConcatenate1 = new StringConcatenate();

commented: Thanks, great explanation! +4

Thanks axeeffect2002 ! Its good to see I am not as rusty as I thought!

One more note on something I missed and I haven't seen anyone else mention from your first post.

which is from class StringConcatenate()

The parentheses here don't really make sense with the phrase in red. StringConcatenate (no parentheses) is the class. StringConcatenate() is a constructor from the StringConcatenate class which creates a new instance of type StringConcatenate . The parentheses imply a method (including constructors), not the class itself.

You probably already knew that, but I figured I'd point it out. Not trying to be overly picky with the grammar or anything.

Hi Vernon, thanks for pointing that out. I tried to remove the parentheses from :

StringConcatenate StringConcatenate1 = new StringConcatenate(); 
StringConcatenate1.stringConcat();

to

StringConcatenate StringConcatenate1 = new StringConcatenate; 
StringConcatenate1.stringConcat();

This gives the error : ( or [ expected.

Hi Vernon, thanks for pointing that out. I tried to remove the parentheses from :

StringConcatenate StringConcatenate1 = new StringConcatenate(); 
StringConcatenate1.stringConcat();

to

StringConcatenate StringConcatenate1 = new StringConcatenate; 
StringConcatenate1.stringConcat();

This gives the error : ( or [ expected.

The code was fine before. Don't change the code. I was referring to the English-sentence description of the code. Make the code the way it was before. You WANT to call the constructor, so you WANT the parentheses, so put the parentheses back in the way they were before.

lol, ok I see exactly what you mean now! Thanks a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.