Solved

how to access a class eveywhere

Posted on 2000-05-09
3
155 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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

773 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