Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using JRuby.

I have many classes implemented in Java, and I want to create objects off of them in my Ruby scripts.

Suppose that I have a class Sprite in Java. In Ruby, to refer to it, I use the following:


Because Sprite is defined in the package com.myWebsite.myProjectName.

Now then, to simplify things, I like to do this:

Sprite = Java::ComMyWebsiteMyProjectName::Sprite

Which sets the constant Sprite to the Sprite class defined in Java. Now my Ruby code can refer to it by just writing Sprite.

I have a file full of that kind of declarations. Something like:

Sprite   = Java::ComMyWebsiteMyProjectName::Sprite
Texture  = Java::ComMyWebsiteMyProjectName::Texture
Bitmap   = Java::ComMyWebsiteMyProjectName::Bitmap
Math     = Java::ComMyWebsiteMyProjectName::Math
Geometry = Java::ComMyWebsiteMyProjectName::Geometry

Now, I am not exactly very familiar with programming concepts. I am having problems giving this file a name, because I don't even know if there is a name for this kind of thing. Does this kind of "practice" have an actual name?

share|improve this question

closed as too broad by MichaelT, Scant Roger, durron597 Dec 7 '15 at 20:38

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

"What is the name of this thing" questions are off-topic. These are poor questions for the same reasons that "identify this obscure TV show, film or book by its characters or story" are bad questions: you can't Google them, they aren't practical in any way, they don't help anyone else, and allowing them opens the door for the asking of other types of marginal questions. See Also Let's Play the Guessing Game – gnat Nov 20 '13 at 11:00
@gnat: I'm not sure, that blog post says that a guessing game is when you have a vague or broad description, but in my case, I am giving an accurate and exact one: what practice is it when you reference large namespaces to shorter constants? It's exact and to the point without loose ends. I'm not giving "hints" - I am giving the full description. – Omega Nov 20 '13 at 11:27
@gnat: Additionally, why would this question be wrong if questions like this other one are fine? The asker describes perfectly fine the concept (so did I), and he is asking what is it called like (so am I). In fact, it seems to me like the answers to that one question are guesses. Unless I am missing the bigger picture here, I feel like this question is as appropriate (or more) than that one. – Omega Nov 20 '13 at 11:27
@GlenH7: Thanks, I ended up choosing the Meta one. You can find it here: – Omega Nov 21 '13 at 21:32
up vote 7 down vote accepted

Generically, this sort of thing is called Type Aliasing. When you're doing it specifically for a namespace, you're importing the namespace (or more accurately, importing the types within the namespace).

share|improve this answer

This is known by many different names - and probably called even more. And sometimes what you think is type aliasing in one language is something completely different in another.

In Perl, you've got code that looks like:


package Foo::Bar::Qux::Bletch;
$var = 42;

package main;
print $Foo::Bar::Qux::Bletch::var, "\n";
*A = *Foo::Bar::Qux::Bletch::var;
print $A,"\n";

Which is known by various things such as a typeglob alias or just glob aliasing - though it should be realized that a glob is something else.

In Clojure, this is simply an alias:

user=> (require 'clojure.string)

user=> (alias 'string 'clojure.string)

user=> (string/capitalize "hONdURas")

In Ruby it appears to have the name of namespace alias as described in ruby-forum: Namespaces too looooooong

However it occurred to me that there is a difference between namespace aliases in PHP and the closest thing in Ruby (as described above): Autoloading.

In php, this is again, a namespace alias - Using namespaces: Aliasing/Importing

The ability to refer to an external fully qualified name with an alias, or importing, is an important feature of namespaces. This is similar to the ability of unix-based filesystems to create symbolic links to a file or to a directory.

PHP namespaces support three kinds of aliasing or importing: aliasing a class name, aliasing an interface name, and aliasing a namespace name. Note that importing a function or constant is not supported.

In Lisp, this isn't part of the standard language but there libraries which do it and call themselves 'package-local alias' or 'nicknames'. One such library is cl-package-aliases which provides some additional functionality:

* (defpackage :foo (:use :cl) (:alias (:common-lisp :bar)))

* (in-package :foo)

* (bar:format t "Hello World~%")
Hello World

In python, this is a namespace alias. (Examples from Can you define aliases for imported modules in Python?)

import a_ridiculously_long_module_name as short_name

Though you can also do it with an assigment

import long_module_name
lmn = long_module_name

In C# this is a namespace alias.

From Java equivalent to C# using alias

namespace TheirNamespace
    public class ContestedClassName

namespace MyNamespace
    public class ContestedClassName

namespace MyBoundaryNamespace
    using MyRenamedClass = MyNamespace.ContestedClassName;
    using TheirRenamedClass = TheirNamespace.ContestedClassName;

    public class Translator
        public TheirRenamedClass Translate(MyRenamedClass value)
            return null;

In Java, one doesn't alias a namespace, but rather imports specific items into the current namespace, though it does serve the question asked (how to reduce the amount of typing):

The code

double r = cos(Math.PI * theta);

can be reduced with a static import to:

import static java.lang.Math.PI;
double r = cos(PI * theta);

One often sees this usage with Junit to avoid having to specify each static method.

import static org.junit.Assert.*;

When people speak of type aliasing in Java it is more along the lines of:

A problem I often encounter in Java is that I want to say “these two things are the same”, but Java won’t let me. Suppose I want to maintain an int[] array which is always sorted in my program.

which gets to more of the type aliasing rather than namespace aliasing, because these are different things.

Type aliasing in Scala appears to be something quite different based on Beginner: Scala type alias in Scala 2.10? and Scala type alias including companion object

More about what type aliasing in Scala is can be found at Effective Scala

In C++11 type aliases are a type of typedef which does more things other than namespace juggling.

using identifier = type_name;   

An example of this would be:

// type alias, identical to
// typedef void (*func)(int, int);
using func = void (*) (int,int);
// the name 'func' now denotes a pointer to function:
void example(int, int) {}
func fn = example;

Though realize, there are some differences between these concepts as descried in Difference between typedef and C++11 type alias

The playing of namespaces however, is a namespace alias as described in In C++, what is a "namespace alias"?

namespace ublas = boost::numeric::ublas;

ublas::vector<double> v;

More on this at msdn namespace Alias

namespace a_very_long_namespace_name { ... }
namespace AVLNN = a_very_long_namespace_name;
// AVLNN is now a namespace-alias for a_very_long_namespace_name. shows a few languages with various forms of namespace aliasing.


module Gr = Graphics;;


import qualified Data.Bytestring as B

Namespaces aren't unique to programming languages, but also extend to other structured data such as XSL which has namespace aliases. These tend to be rather terse transformations (converting 'alt' to 'xsl' or something like that - but they serve the same purpose.

So, as noted, these concepts go by different names in different languages and what is known as 'type aliasing' in one language may mean something completely different in another language.

There is no standard name

share|improve this answer

This is usually called "bindings". For example, there are many node.js modules that are just bindings with C++ libraries. Or Python libraries that provide bindings for C code. And there are probably some JRuby libraries that provide bindings for Java code.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.