?
Solved

how to access a class eveywhere

Posted on 2000-05-09
3
Medium Priority
?
166 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
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

864 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