Solved

how to access a class eveywhere

Posted on 2000-05-09
3
158 Views
Last Modified: 2010-04-01
Can anyone tell me what I am doing wrong
say if I do this
 
 
        try{
                RandomAccessFile fxxx = new RandomAccessFile("test", "rw");
        }
        catch(Excption e){
                System.out.println("Error: " + e.toString());
        }
 
and later on try
 
fxxx.write(buf);
 
It will tell me fxxx is a unknow class, If
I try it right after I create it I can acess it
So what I want to know is how to acess fxxx everwhere in the program
 
Thanks for any help.
0
Comment
Question by:thedogeater
[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
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 2794374
The variable fxxx will need to both persist and gain wider scope.

First persistence. As it is declared the variable fxxx will go out of scope, which will cause it to cease to exist, as soon as execution leaves the try block. To "survive" beyond its current block or method, a variable must be either be an "instance" variable or a "class" variable. If a variable is an "instance" variable a distinct copy of it exists in each "instance" of the class. It would be declared:

class FileAcc
{   RandomAccessFile fxxx;

    try
    {   fxxx=new RandomAccessFile("test","rs");
    ...
    ...

If it is a "class" variable, one copy of it exists regardless of how many instances of the class exist (or any at all). In this case you declare it as:

class FileAcc
{   static RandomAccessFile fxxx;

    try
    {   fxxx=new RandomAccessFile("test","rw");
    ...
    ...

An instance variable is accessed with a reference to the specific class instance:

class someother
{   FileAcc fa=new FileAcc();

    fa.fxxx  // is the reference here

For a class variable you can access it with:

    FileAcc.fxxx

To make it accessible in other packages you will have to add the "public" keyword to the declaration, and possibly import the package "FileAcc" is in. If you do everything in the default package this need not concern you.
0
 
LVL 16

Expert Comment

by:imladris
ID: 2797023
Is there any further clarification needed before you grade the answer?
0
 

Author Comment

by:thedogeater
ID: 2805871
Thanks a lot
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

729 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