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.

Results 1 to 6 of 6

Thread: call another class

  1. #1
    Member
    Join Date
    May 2020
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default call another class

    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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: call another class

    how to call another public class from another public class
    Use a new statement to create the class and get a reference to the class. Then use that reference to call method:
      TheClass 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.

  3. #3
    Member
    Join Date
    May 2020
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: call another class

    Quote Originally Posted by Norm View Post
    Use a new statement to create the class and get a reference to the class. Then use that reference to call method:
      TheClass 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.
    @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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: call another class

    i want to call above class "mqTemplate "
    Are you referring to calling the class's constructor when creating an instance of the class?

    Did you get any error messages? Please copy the full text and paste it here.

    what is @component, @Autowired, @Override
    I do not recognize the first two annotations.
    @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.

  5. #5
    Member
    Join Date
    May 2020
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: call another class

    Quote Originally Posted by Norm View Post
    Are you referring to calling the class's constructor when creating an instance of the class?

    Did you get any error messages? Please copy the full text and paste it here.


    I do not recognize the first two annotations.
    @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]
    @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()
    }
    }

    }

    }
    [/code]

    to get highlighting and preserve formatting.
    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: call another class

    is it correct?
    It could be. Did you try it to see what happens?

    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.

Similar Threads

  1. Incorrect call to class?
    By Drekinn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 29th, 2013, 06:28 PM
  2. Call class method from another class
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 1
    Last Post: November 21st, 2012, 06:56 AM
  3. How do I call a class in a main class
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2011, 03:11 AM
  4. How to call int value from another class
    By IDK12 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 14th, 2011, 09:18 PM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM