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
Solved

Issues with the scope of a variable

Posted on 2012-03-18
4
250 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

856 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