Solved

Issues with the scope of a variable

Posted on 2012-03-18
4
252 Views
Last Modified: 2012-08-13
Hi,

In the attached file I have the following function which is a void. But I want to store the max_session_state in such a way that in can be accessed globally anywhere in the code.

Or can can be a protected class member of a new class say SessionTask class but I should be able to access this from an accessor method anywhere in the code

I am hitting a dead end here. Any help ?  Also can someone point me to a really good tutorial on scope of a variable and how to handle multiple scenarios with it


thanks
-anshu



public void characters(char ch[], int start, int length) throws SAXException {
 
            
            if (max_state) {
                  
                              
                  //System.out.println("Max Session State : " + new String(ch, start, length));
                  
                  String max_session_state=new String(ch, start, length);
                  
                  System.out.println("Max Session State : " + max_session_state );
                  
                  max_state = false;
            }
java-scop.txt
0
Comment
Question by:anshuma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Assisted Solution

by:AllThingsJava
AllThingsJava earned 125 total points
ID: 37735014
//Do something like:

class WhatEverClassThisThatMethodCameFrom {

private String max_session_state;

public String getMaxSessionState() {
     return max_session_state;
}

public void characters(char ch[], int start, int length) throws SAXException {
 
            if (max_state) {
                 
                             
                  //System.out.println("Max Session State : " + new String(ch, start, length));
                 
                  max_session_state=new String(ch, start, length);
                 
                  System.out.println("Max Session State : " + max_session_state );
                 
                  max_state = false;
            }
}
}
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 125 total points
ID: 37735016
If you want a variable this variable to be accessed outside this method also, then declare this variable as a class member variable
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 125 total points
ID: 37735075
Thewse aressimple tutorials about variable scope in Java


http://maiaco.com/articles/java/scope.php


http://www.java-made-easy.com/variable-scope.html

http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html




>Or can can be a protected class member of a new class say SessionTask class but I should be >able to access this from an accessor method anywhere in the code

If you want to aceess your class member anywere in your code then make this member public

protected members can be accessed in the classes which are subclasses of the current class
0
 
LVL 7

Assisted Solution

by:micropc1
micropc1 earned 125 total points
ID: 37735081
You might want to try doing some re-coding. Not really the best idea to put everything in main. Try something like this...  

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
 
public class GetXmlHttp {
	
	private SAXParser saxParser;
	private SAXParserFactory factory;
	private DefaultHandler handler;
	
	private class SAXHandler extends DefaultHandler {
		
		private String max_session_state = "";
		private boolean max_state = false;
		
		public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException {
			System.out.println("Start Element :" + qName);
		 
			if (qName.equalsIgnoreCase("max-state")) {
				max_state = true;
			}
		}
		 
		public void endElement(String uri, String localName, String qName) throws SAXException {
			System.out.println("End Element :" + qName);
		}
		 
		public void characters(char ch[], int start, int length) throws SAXException {
			if (max_state) {
				max_session_state=new String(ch, start, length);
				System.out.println("Max Session State : " + max_session_state );
				max_state = false;
			}
		}
	}
	
	public GetXmlHttp() throws ParserConfigurationException, SAXException {
		this.factory = SAXParserFactory.newInstance();
		this.saxParser = factory.newSAXParser();
	 	this.handler = new SAXHandler();	 
	}
	
	public void parse(InputSource myInputSource) throws SAXException, IOException {
		this.saxParser.parse(myInputSource, this.handler);
	}
	
	public static void main(String argv[]) throws MalformedURLException, IOException, SAXException, ParserConfigurationException {
		String url="http://mydummyur.com.test.com";
		GetXmlHttp mySaxParser = new GetXmlHttp();
		mySaxParser.parse(new InputSource(new URL(url).openStream()));
	} 
}

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 87
spring jars download 1 53
sql import cannot be resolved jsp 3 76
Java program running SQL query 5 50
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question