may I know how to call another public class from another public class , I try to import and then call another class, it seems cant do that
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
may I know how to call another public class from another public class , I try to import and then call another class, it seems cant do that
Use a new statement to create the class and get a reference to the class. Then use that reference to call method:how to call another public class from another public classTheClass theClass = new TheClass(); // create instance and set reference theClass.theMethod(); // call a method in the other class
Post any code that you are having problems with. Also copy and paste any error messages here.
Note: You do not call classes, you call methods in a class.
If you don't understand my answer, don't ignore it, ask a question.
@component
public class anotherClass implements ownClass {
@Autowired
private MqMsgModel mqMsgConvertor;
@Override
public mqMsg mqTemplate (MqMsg mqMsg) {
try {
mqMsg.getMsgHeader().setTime(new Date());
catch( Exception e) {
e.printStackTrace()
}
}
}
}
what is @component, @Autowired, @Override from above coding? what is function ? and what type of java programming?
i want to call above class "mqTemplate " by passing parameter "mqMsg" from another public class, how can i do that?
Are you referring to calling the class's constructor when creating an instance of the class?i want to call above class "mqTemplate "
Did you get any error messages? Please copy the full text and paste it here.
I do not recognize the first two annotations.what is @component, @Autowired, @Override
@Override is used when extending a class or implementing an interface and you want to ask the compiler to check if your method override is correct.
Read the tutorial about annotations: https://docs.oracle.com/javase/tutor...ons/index.html
Please edit your post and wrap your code with code tags:
[code]
**YOUR CODE GOES HERE**
[/code]
to get highlighting and preserve formatting.
If you don't understand my answer, don't ignore it, ask a question.
if i want to call above class from another public class, i just create instance and call its method.
here is my coding.
anotherClass tryClass= new anotherClass (); // create instance and set reference mqMsg testMsg=tryClass.mqTemplate (mqMsg); // call a method in the other class
[code in another class] is it correct?
Last edited by Norm; June 16th, 2020 at 10:55 AM. Reason: Fixed code tags
It could be. Did you try it to see what happens?is it correct?
Please wrap all posted code in code tags.
There is one confusing bit in the code
mqMsg
is shown as both the name of a class and the name of a variable.
If you don't understand my answer, don't ignore it, ask a question.