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

Thread: How to create a pointer type of effect for objects

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to create a pointer type of effect for objects

    Hi I have a class with two methods referring a session object. I want to modify it and make it work to handle two sessions running parallely. So based if a new session comes within a session I want to assign the new session to another variable. You know I don't want to modify the class severely. I just want to use a boolean variable that checks the status and assign the session to one of the two variables and the res of the logic will run smoothly. Here is the frameowrk of my class
    Public class MyClass
    {
            private IoSession clientSession = null; 
    	private IoSession serverSession = null;
            private IoSession newServerSession = null;
     
    public void passToServer( RtspMessage message )
    	{
    //I include the following assignment statment and if statement to make is work for two sessions
     
                   /* IoSession serverSession =null;
                     if (this.sessionWithinASession==true)
                    {
                        serverSession=this.newServerSession; //This is my question
                    }
                    else
                    {
                        serverSession=this.serverSession; //This is my question. I want the variable serverSession (the local one to point to the two global variables). So, tell me if I'm wrong and how I can implement.
     
    ...
     
    if ( serverSession == null ) {
    				RtspRequest request = (RtspRequest) message;
    				try {
    					connectServerSide( request.getUrl() );
     
    				} catch ( IOException e ) {
    					log.error( e );
    					// closeAll();
    				} finally {
    					if ( serverSession == null )
    						return;  //I know connectServerSide Creates a new serversession and assigned it to the external one but it terminates b/c this is not referring the global one
    				}
    			}
    }
     
    private void connectServerSide( URL url ) throws IOException
    	{
                //This is to also another code that i included for the same reason that I mentioned above
                    IoSession serverSession =null;
                    if (this.sessionWithinASession)
                        serverSession=this.newServerSession;
                    else
                        serverSession=this.serverSession;
     
    ......
    	// Create TCP/IP connector.
    		SocketConnector connector = new SocketConnector(); 
    		// Start communication.
    		log.debug( "Trying to connect to '" + host + "' " + port + " URL is "+url);
    		try {
     
    		ConnectFuture future = connector.connect(
    					new InetSocketAddress( host, port ), new ServerSide(),
    					new RtspServerFilters() );
    			future.join();
    			serverSession = future.getSession(); //I suppose this will modify the local and also the global and was thinking that by the time it gets back to the previous method serverSession will not be null
     
    		} catch ( UnresolvedAddressException e ) {
     
    		    return;
    		}
     
    }
    So as you see the connect server side creates a session and should assign to the proper global variable. But in PassToServer method after the control gets back serverSession (which I think is refering to either the global serversession and global newserversession is still null). I hope my question is clear. Basically my question is how can I point an object from a method using the same type of object and modify the pointer and get the effect on the global object.

    Thank you




    }
    Last edited by copeg; October 19th, 2010 at 08:47 AM.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: How to create a pointer type of effect for objects


  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to create a pointer type of effect for objects

    Sorry I'm new to this forum and thought that the posting I made was not the proper forum and made the other posting. It is not that I want to be smart.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to create a pointer type of effect for objects

    Ok, well I say this a lot. There is no good reason to have local variables and global variables have the same name. That sort of thing just creates the kind of confusion you are having here. With that said, your attempt to set the global variable to be equal to the local variable is flipped. This:
    serverSession=this.serverSession;
    should be:
    this.serverSession=serverSession;

    The keyword this is used to reference the class you are in, not the method you are in. Based on basic programming, the variable you are setting is on the left of the equals sign and the value you are assigning said variable is on the right of the equals sign. Which means, your call to this.serverSession needs to be on the left, as you are attempting to assign that value.

    My best advice to you is to not have local and global variables with the same name, this can cause some confusion in your programs and serves no benefit. What changing the local variable name would look like is:
    Instead of this:
    Public class MyClass
    {
            private IoSession clientSession = null;
        private IoSession serverSession = null;
            private IoSession newServerSession = null;
     
    public void passToServer( RtspMessage message )
        {
    //I include the following assignment statment and if statement to make is work for two sessions
     
                   /* IoSession serverSession =null;
                     if (this.sessionWithinASession==true)
                    {
                        this.serverSession=newServerSession; //This is my question
                    }
                    else
                    {
                        this.serverSession=serverSession; //This is my question. I want the variable serverSession (the local one to point to the two global variables). So, tell me if I'm wrong and how I can implement.
     
    ...
    You would have this:
    public class MyClass
    {
            private IoSession clientSession = null;
        private IoSession serverSession = null;
            private IoSession newServerSession = null;
     
    public void passToServer( RtspMessage message )
        {
    //I include the following assignment statment and if statement to make is work for two sessions
     
                    IoSession serverSession2 =null;
                     if (sessionWithinASession==true)
                    {
                        serverSession=newServerSession; //This is my question
                    }
                    else
                    {
                        serverSession=serverSession2; //This is my question. I want the variable serverSession (the local one to point to the two global variables). So, tell me if I'm wrong and how I can implement.
     
    ...

    Going from that, I have more questions:
    1) Where is the variable sessionWithinASession?
    2) If it is a boolean, why bother compare to true? If you are checking if boolean sessionWithinASession is true, you only need to say: if (sessionWithinASession) as sessionWithinASession will return a true or false value, which is what an if statement looks for.
    3) Inside the if statement I mentioned above, are you attempting to set the global variable serverSession or the local variable serverSession?
    4) Why do you have 3 IoSessions? What is the purpose of having 3 instead of 2? 1 for the client and 1 for the server.
    Last edited by aussiemcgr; October 19th, 2010 at 10:21 AM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: How to create a pointer type of effect for objects

    Quote Originally Posted by aussiemcgr View Post
    Ok, well I say this a lot. There is no good reason to have local variables and global variables have the same name.
    Agree. With the exception of method or constructor parameters that correspond to the field whose value is to be set from them.

    your attempt to set the global variable to be equal to the local variable is flipped. This:
    serverSession=this.serverSession;
    should be:
    this.serverSession=serverSession;
    Actually, it isn't and it shouldn't. The OP is apparently conditionally setting the local variable equal to one of two instance fields.

    And in Java, the nearest thing to a global variable is a static (and usually final) class field, or an interface field. An instance field is, well, an instance field, but may equally well be referred to just as a field.

    db

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to create a pointer type of effect for objects

    Hi first of all sorry for the late reply. It is b/c of time difference. I thank you for your advice. And here are my responses to your questions:

    Quote Originally Posted by aussiemcgr View Post
    Ok, well I say this a lot. There is no good reason to have local variables and global variables have the same name. That sort of thing just creates the kind of confusion you are having here. With that said, your attempt to set the global variable to be equal to the local variable is flipped. This:
    serverSession=this.serverSession;
    should be:
    this.serverSession=serverSession;
    You know I didn't want to change the implementation of the original code. That is the reason why I used the same variable name. Anyway, you are right, it creates confusion. The assignment should also be different. BTW, let me explain what I'm trying to do (as you have also asked me why I need three I/O sessions). The application I'm trying to modify is a streaming application (a proxy) and I want to change the media I get from the server based on a certain condition. The proxy just mediates b/n them. So, when the condition fulfils, I want to create another connection to the server while the client gets the previously established stream and once the second session is established (and teh setup is complete), i'll stop the first one and start accepting the second one and send on the new media on the same client session to the client. I want to use the same connection to the client and don't want to establish a new connection. This will create a good user experience and also you can apply the application to any kind of client. I hope I made it clear (my idea).

    Going from that, I have more questions:
    1) Where is the variable sessionWithinASession?
    2) If it is a boolean, why bother compare to true? If you are checking if boolean sessionWithinASession is true, you only need to say: if (sessionWithinASession) as sessionWithinASession will return a true or false value, which is what an if statement looks for.
    yes it is a silly mistake. I think i mkodifed that later.
    3) Inside the if statement I mentioned above, are you attempting to set the global variable serverSession or the local variable serverSession?
    4) Why do you have 3 IoSessions? What is the purpose of having 3 instead of 2? 1 for the client and 1 for the server.
    I explained it above.

    But you know my problem is I want to use this local variable in two different methods (PassToServer and ConnectServerSide) and want to affect either serverSession or newServerSession. So, this is my biggest proble. I'll try to modify the code as per you sugegstion and come back to you.

    Thanks again.

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to create a pointer type of effect for objects

    Quote Originally Posted by Darryl.Burke View Post

    The OP is apparently conditionally setting the local variable equal to one of two instance fields.
    And in Java, the nearest thing to a global variable is a static (and usually final) class field, or an interface field. An instance field is, well, an instance field, but may equally well be referred to just as a field.
    db
    Thanks. This is a good information. By the way as I said earlier, I want to modify the local variable hoping to affect the global (as a side effect).
    Last edited by zelalem; October 20th, 2010 at 02:58 AM.

Similar Threads

  1. Create Objects on Demand?
    By Programmierer in forum Object Oriented Programming
    Replies: 5
    Last Post: July 7th, 2010, 01:13 PM
  2. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  3. HttpURLConnection null pointer exception
    By chopficaro in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2010, 10:19 AM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM

Tags for this Thread