android studio accepted importing inner class? - java

i have this code :
package com.example.android.cars.data;
public final class DataBaseContract {
public static final class Table1Entry implements BaseColumns {
/** Name of database table for cars */
public final static String TABLE_NAME = "car";
}
}
i use Table1Entry in another class with different package and i import the nested class like this
import com.example.android.cars.data.DataBaseContract.Table1Entry;
this allow me to use nested class without outer prefix DataBaseContract,
my question is when i removed static from nested class the code still work, how can this accrue in this case !! i need outer instance to access it!!

Yes you would need an instance of the outerclass IF you wanted to access instance methods of the inner class. However from your example you are only accessing static fields, therefore because the field is static you can access it directly like you explained.

Related

What is the actual purpose of that nested class?

I am a newbie in Android programming and have little confusion in following code.
This portion of code is from android developer site:
public final class FeedReaderContract {
// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
public FeedReaderContract() {}
/* Inner class that defines the table contents */
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
...
}
}
I read some articles about nested classes in Java documentation and some other blogs and understood following reasons to use nested class:
Provides better encapsulation.
If a class is concerned with only one another class, then we better would make nested class of these two.
But here, I could not figure out anything to use nested class.
What exactly is the purpose of making nested class?
This particular nested class is used as a namespace for String constants related to its main class.
It is abstract, so it's not intended for instantiation
It implements BaseColumns interface, which is essentially a way to "dump" string constants into the FeedEntry class
Why did they used nested class instead of free-standing class?
Use of a nested class underscores "semantic closeness" of the inner class to its outer class. It cnveys to the readers of your code that FeedEntry can be interpreted only in the context of FeedReaderContract. Otherwise, FeedEntry is meaningless. Essentially, this is done for the human readers of code, because in this case there would be no difference to the compiler.

meaning of protected static A.B newvar = null

I was going throught a huge java project and I came across this line in a file.I am new to java and don't know what this means.Or more specifically
Should I look at PSStreamer.java OR Client.java to see the methods and member variables of the below object.
protected static PSStreamer.Client packetClient = null;
This is what's being declared:
protected // protected visibility modifier
static // a class (static) member
PSStreamer.Client // Client is an inner class of PSStreamer
packetClient = null; // variable name, null initial value
You should look inside PSStreamer to find the inner class Client, and that's where you'll find the attributes and methods of packetClient.
That is a static inner class.
It would look like this: (in PSStreamer.java):
class PSStreamer {
...
static class Client {
...
}
}
That is a static nested class. It should be defined in the source code as
public class PSStreamer {
public static class Client {
// ..
}
// ..
}
So, you should be looking inside PSStreamer.java. Read more about Nested Classes.
Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.
Also, take a look at this SO link: Java inner class and static nested class

Java function (method) available everywhere (global)

I've recently (4 days ago) started programming in JAVA. I have some overall programming experience from C++ and PHP. My question is: can we implement a function in JAVA, that is available in all classes? I'm thinking of some global logging function, that I need to call in several places (log events, errors, etc.).
Imagine I have two classes, A and B. I need to call logging function in both of them, but I don't want to copy whole function body (awful thing I believe), and I want to call it strict (without creating another class, instantiating it, and then calling from the instance), like logEvent(someVariable). So I should use an abstract class C, which A and B will extend, BUT they are already an extension of other class (built-in). Since multiple inheritance isn't allowed (is it?), I need to do some trick. Singleton is not pleasing me too. In PHP or C++ I would just create separate file with function body and then include it.
Here is how I want to use it:
public class A extends SomeClass {
String error = "Error from class A";
logEvent(error);
}
public class B extends SomeOtherClass {
String error = "Error from class B";
logEvent(error);
}
Put a static method in any class (it could be a utils class, or whatever), then call it like this: ClassName.functionName()
Static methods belong to the class, not instances of the class, so you don't need to instantiate the class to access the method
But everything in Java has to be in a class, so you can't access it without the class name.
You should use static method:
package xxx;
public class Util{
public static void logEvent(String error){
...
}
}
and import static:
import static xxx.Util.*;
public class A extends SomeClass {
String error = "Error from class A";
logEvent(error);
}
You may use static method.
Define a class with a static method:
public class Util{
public static void logEvent(String error){
...
}
}
Then, you can use static metod like this way:
public class A extends SomeClass {
String error = "Error from class A";
Util.logEvent(error);
}
you may take a look here to learn more about static method, http://www.leepoint.net/notes-java/flow/methods/50static-methods.html

Java - Declare Class Once Use Anywhere

Very simple problem but im not understanding static correctly.
I have java file which holds my main and its call testMain.
With my testMain it makes many classes with use other classes.
E.g. testMain>>GUI and testMain>>model and testMain>>controller
Now i have a class called generatorTester which i would like to declare once like:
public static utils.generatorTester randomGen = new utils.generatorTester ();
(utils is my custom package for my common classes)
Why does the above line not aloud me to do the following
classNameOfMainFunction.randomGen
Im i programming wrong here? Is this even possbile.
I bassicly want to make the class globably and use it any where.
A public static field of a public class can be used anywhere, you just need to use the right syntax to access it.
If you declare:
package foo;
public class Global {
public static Some thing;
}
And do
import foo.Global;
you can access the field with
Global.thing
Alternatively, you can do
import static foo.Global.thing;
and access it with
thing
About the best you can get is this:
public abstract class GloballyUsed {
public static int method() { return 4;
/* determined by fair
* dice roll, guaranteed to be random */
}
and:
GloballyUsed.method();
to call elsewhere.
Note per comment (I just learned this) since Java 5 you can import just a specific method name as:
import static {package}.GloballyUsed.method;
Note I added the keyword abstract, this is to further convince you that you never actually instantiate GloballyUsed. It has no instances. You probably have some reading to do on what static means.

jMockit's access to a private class

I have a public class with a private class inside it:
public class Out
{
private class In
{
public String afterLogic;
public In(String parameter)
{
this.afterLogic = parameter+"!";
}
}
}
And wanted to test the In class with jMockit. Something along these lines:
#Test
public void OutInTest()
{
Out outer = new Out();
Object ob = Deencapsulation.newInnerInstance("In", outer); //LINE X
}
The problema is, in LINE X, when trying to cast ob to In, the In class is not recognized.
Any idea how to solve this?
Thanks!
The only constructor in class In takes a String argument. Therefore, you need to pass the argument value:
Object ob = Deencapsulation.newInnerInstance("In", outer, "test");
As suggested in the comment one way is to change the access modifier of the inner class from private to public.
Second way (in case you don't want to make your inner class public), you can test the public method of outer class which is actually calling the inner class methods.
Change the scope of the inner class to default then make sure that the test is in the same package.
There are two approaches, first as mentioned in other posts to change the scope to public. The second which I support is, to avoid testing private class altogether. Since the tests should be written against testable code or methods of the class and not against default behavior.

Categories