Solved

how to access a class eveywhere

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

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 263
ejb wildfly example 2 106
ejb entity bean example issue 2 40
Eclipse neon2 "Java build path" correctness 7 43
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 contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

737 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