Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  

AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

public: interface access


+ Font mai mare | - Font mai mic

public: interface access

When you use the public keyword, it means that the member declaration that immediately follows public is available to everyone, in particular to the client programmer who uses the library. Suppose you define a package dessert containing the following compilation unit: (See page 97 if you have trouble executing this program.)


// Creates a library

package c05.dessert;

public class Cookie

void foo()

Remember, must reside in a subdirectory called dessert, in a directory under C05 (indicating Chapter 5 of this book) that must be under one of the CLASSPATH directories. Don't make the mistake of thinking that Java will always look at the current directory as one of the starting points for searching. If you don't have a '.' as one of the paths in your CLASSPATH, Java won't look there.

Now if you create a program that uses Cookie:


// Uses the library

import c05.dessert.*;

public class Dinner

public static void main(String[] args)

You can create a Cookie object, since its constructor is public and the class is public. (We'll look more at the concept of a public class later.) However, the foo( ) member is inaccessible inside since foo( ) is friendly only within package dessert.

The default package

You might be surprised to discover that the following code compiles, even though it would appear that it breaks the rules:


// Accesses a class in a separate
// compilation unit.

class Cake

In a second file, in the same directory:


// The other class

class Pie

You might initially view these as completely foreign files, and yet Cake is able to create a Pie object and call its f( ) method! You'd typically think that Pie and f( ) are friendly and therefore not available to Cake. They are friendly - that part is correct. The reason that they are available in is because they are in the same directory and have no explicit package name. Java treats files like this as implicitly part of the "default package" for that directory, and therefore friendly to all the other files in that directory.

Politica de confidentialitate



Vizualizari: 571
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2023 . All rights reserved