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 9 of 9

Thread: How to make operational objects?

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to make operational objects?

    I'm a beginner, so, please be patient

    I'm trying to make messages between classes coherent.
    Hence, if I'm instancing a class I want to be able to declare the variable storing the instance by the class name it is installing.

    Following a MVC design pattern, but omitting the view (not relevant for this purpose) I have got the next self-explanatory code:

    Model class
    /**
     * 
     */
    package model;
     
    import java.lang.*;
    import java.io.*;
     
    /**
     * @author jonathan
     *
     */
    public class UsersM {
     
    	UsersM users;
     
    	/**
    	 * 
    	 */
    	public UsersM() {
    		// TODO Auto-generated constructor stub
    	}
     
    	public UsersM get() {
    		return users;
    	}
     
    }

    Controller class
    /**
     * 
     */
    package controller;
     
    import model.UsersM;
     
    /**
     * @author jonathan
     *
     */
    public class UsersC {
     
    	/**
    	 * 
    	 */
    	public UsersC() {
    		// TODO Auto-generated constructor stub
    	}
     
    	public UsersM get() {
    		return new UsersM().get();
    	}
     
    }

    Main class
    /**
     * 
     */
    package Main;
     
    import controller.*;
    /**
     * @author jonathan
     *
     */
    public class Main {
     
    	/**
    	 * 
    	 */
    	public Main() {
    		// TODO Auto-generated constructor stub
    	}
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		UsersC users = new UsersC();
     
    		System.out.println(users.get());
    	}
     
    }
     
    //The output after run the code is null.

    which does nothing because I'm not able to actually store anything in the variables, and also I'm forced to limit the get() method to the type of variable I'm retrieving the message from. I would like to know if there is a reasonable way work around this because I look for examples and documentation but I cannot find anything about this.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to make operational objects?

    I don't understand what you are trying to do. Can you explain?
    Are there compiler errors you are having problems with? If so copy the full text and paste it here.
    If the code executes, does it do what you want? If not, please explain what you want it to do.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make operational objects?

    Quote Originally Posted by Norm View Post
    I don't understand what you are trying to do. Can you explain?
    Are there compiler errors you are having problems with? If so copy the full text and paste it here.
    If the code executes, does it do what you want? If not, please explain what you want it to do.
    Many thanks for your answer. No, the code works, returns null as it is expected. But, it has to return null because the variables cannot store anything due to I can't assign them any additional type further than the one they have.

    The purpose of this is to achieve a total abstraction, I want to not have to open the class I am retrieving to check what type is returning.
    Is this a wrong way? If yes, is there a better way?
    Also, with this I am looking to achieve a more readable code.
    Last edited by ClipOS; March 3rd, 2022 at 10:15 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to make operational objects?

    the variables cannot store anything
    Please explain why not.

    open the class I am retrieving to check what type is returning.
    Can you explain what that means?
    What does "open the class" mean?
    What does "type is returning" mean? Java requires that methods specify what data type they are returning. True, some methods return data of type Object but that is often a poor design.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make operational objects?

    Quote Originally Posted by Norm View Post
    Please explain why not.
    Because once I declare a variable type Class I cannot assign values type Set, List, ArrayList to it, it throws me a mismatch type error.

    Quote Originally Posted by Norm View Post
    Can you explain what that means?
    What does "open the class" mean?
    What does "type is returning" mean? Java requires that methods specify what data type they are returning. True, some methods return data of type Object but that is often a poor design.
    It means I have to open the class with the IDE (Eclipse in my case) and check what the method returns in order to make the variable compatible with that object. And that is not really abstraction, is it?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to make operational objects?

    once I declare a variable type Class I cannot assign values type ...
    Yes that is the way java works.
    If you declare variables as type Object, then any class's object can be assigned to that variable because all classes extend Object.
    However you can not assign primitives like int or char to that variable.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2022
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make operational objects?

    Quote Originally Posted by Norm View Post
    Yes that is the way java works.
    If you declare variables as type Object, then any class's object can be assigned to that variable because all classes extend Object.
    However you can not assign primitives like int or char to that variable.
    Not primitives, but, how about data structures like Set, List, ArrayList, HashMap? Any book to read that talks about all this? I have not clear many things.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to make operational objects?

    I still do not know what you are trying to do. Java requires types to be compatible.

    Look at the tutorial for the word type: https://docs.oracle.com/javase/tutor...ybigindex.html
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2022
    Posts
    15
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Talking Re: How to make operational objects?

    Quote Originally Posted by ClipOS View Post
    ...I want to not have to open the class I am retrieving to check what type is returning...
    ...Not primitives, but, how about data structures like Set, List, ArrayList, HashMap? ...
    I am not sure you got an answer already but I wrote my answer below :

    Because of two statements above, I ended up that : "you want to check what is the returning object's class, whether it is Set or ArrayList or HashMap etc." am I right?

    If I am right, then ; there is an instanceof() operator in Java , and you can find more on https://docs.oracle.com/en/java/java...-operator.html. In that page you can find that different classes defined for shape and which one of them returned determined by instanceof() operator. You can do similar for Set, ArrayList or any other classes I think. Also classes on the link(i.e Rectangle,Circle tc.) are all derived from(in exact word implemented from) an abstract class(i.e. interface) Shape. You can catch and follow this hint/tip and you can go further from there.

    However, if you want to do something else, both my assumption and answer is not valid.

Similar Threads

  1. How to make an array list of objects in another object?
    By bonder in forum Collections and Generics
    Replies: 2
    Last Post: January 15th, 2019, 07:36 AM
  2. How can I make a JDialog to make main Frame to "sleep"
    By piulitza in forum AWT / Java Swing
    Replies: 1
    Last Post: May 11th, 2012, 08:00 AM
  3. To Make class immutable which has ref to other mutable objects
    By tcstcs in forum Object Oriented Programming
    Replies: 1
    Last Post: May 4th, 2012, 10:42 AM
  4. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  5. Basic Operational Question on Tomcat/Servlets
    By dottore11 in forum Java Servlet
    Replies: 1
    Last Post: December 24th, 2010, 04:11 AM